Run Javascript function only if 'beforeunload' function returns false

H.W.

Is there a way to execute javascript function if user click "not leave" in beforeunload alert?

I use JQuery and the following code:

$(window).on('beforeunload', function (e) {
    if (formSubmitting || !isDirty()) {
        return undefined;
    }
    var confirmationMessage = 'You have unsaved work.';
    (e || window.event).returnValue = confirmationMessage; //Gecko + IE
    return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
});

So how to check, if user stays on the page or leave it?

frogatto

When beforeunload confirmation dialog is shown to the user, there are two possibilities:

  • User leaves the page: in this case unload event is fired.

  • User stays on page: nothing will happen.

So, leaving can be detected by unload event, however staying cannot be detected by an event.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related