I have written this code
function winUnload() {
alert("Unload Window");
MyMethod();
}
window.onunload = function() { winUnload(); }
This code is working fine in IE and Firefox. But this code is not working in Chrome. Both the statements alert("Unload Window");
and MyMethod();
are not working.
onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.). Note: The onunload event is also triggered when a user reloads the page (and the onload event).
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 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. The default message that appears in the confirmation box, is different in different browsers.
There are some actions which are not working in chrome, inside of the unload event. Alert or confirm boxes are such things.
But what is possible (AFAIK):
Example for #2:
$(window).on('beforeunload', function() { return 'Your own message goes here...'; });
Demo: http://jsfiddle.net/PQz5k/
I know this is old but I found the way to make unload work using Chrome
window.onbeforeunload = function () { myFunction(); };
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