Is it possible to capture user response when using onbeforeforeload confirmation?

I need to warn users that they will lose information when they exit the page. This is easy to do using the onbeforeunload event. My problem is that I want to take some action if the user decides to leave.

Here is an example (I use jquery because it is loaded anyway):

$(window).on('beforeunload', function(e){
    return "Do you really want to leave?";
});

What I would like to do is something like this (this code does not work, I know, this is just an example to illustrate what I'm trying to do):

$(window).on('beforeunload', function(e){
    // Ask for user confirmation
    var bUserAnswer = confirm("Do you really want to leave?");

    if(bUserAnswer)
    {
        // Do something...
    }
    else
    {
        // Do something else...
    }
    // Close everything if the user decides to leave...
    return bUserAnswer;
});

I have no idea that what I'm trying to do here is even possible ... Walking around did not give me any directions anyway, so I turn to my favorite group of experts!

Any idea how I could do this?

Thank!

+5
1

beforeunload unload . , beforeunload , unload .

, beforeunload , - " " " " ( , , ).

, , ​​, unload - beforeunload ().

, , , unload.

, , , :)

+10

All Articles