I am popping up a dialog box when someone tries to navigate away from a particular page without having saved their work. I use Javascript's onbeforeunload event, works great.
Now I want to run some Javascript code when the user clicks "Cancel" on the dialog that comes up (saying they don't want to navigate away from the page).
Is this possible? I'm using jQuery as well, so is there maybe an event like beforeunloadcancel I can bind to?
UPDATE: The idea is to actually save and direct users to a different webpage if they chose cancel
Cancelable: The beforeunload event can be canceled by user interaction: // by https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload#Example window. addEventListener("beforeunload", function(event) { event. preventDefault(); // Cancel the event as stated by the standard.
The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page.
onbeforeunload Below are my findings on the iPad; Using window. onunload , I am able to get an alert when user navigates to a different page from myPage. html (either by clicking on some link or doing a Google search while on myPage.
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.
You can do it like this:
$(function() { $(window).bind('beforeunload', function() { setTimeout(function() { setTimeout(function() { $(document.body).css('background-color', 'red'); }, 1000); },1); return 'are you sure'; }); });
The code within the first setTimeout
method has a delay of 1ms. This is just to add the function into the UI queue
. Since setTimeout
runs asynchronously the Javascript interpreter will continue by directly calling the return
statement, which in turn triggers the browsers modal dialog
. This will block the UI queue
and the code from the first setTimeout
is not executed, until the modal is closed. If the user pressed cancel, it will trigger another setTimeout which fires in about one second. If the user confirmed with ok, the user will redirect and the second setTimeout is never fired.
example: http://www.jsfiddle.net/NdyGJ/2/
I know this question is old now, but in case anyone is still having issues with this, I have found a solution that seems to work for me,
Basically the unload
event is fired after the beforeunload
event. We can use this to cancel a timeout created in the beforeunload
event, modifying jAndy's answer:
$(function() { var beforeUnloadTimeout = 0 ; $(window).bind('beforeunload', function() { console.log('beforeunload'); beforeUnloadTimeout = setTimeout(function() { console.log('settimeout function'); $(document.body).css('background-color', 'red'); },500); return 'are you sure'; }); $(window).bind('unload', function() { console.log('unload'); if(typeof beforeUnloadTimeout !=='undefined' && beforeUnloadTimeout != 0) clearTimeout(beforeUnloadTimeout); }); });
EDIT: jsfiddle here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With