unload function in jQuery works fine in Firefox but not in chrome and safari. please check this fiddle in chrome and Firefox. http://jsfiddle.net/jeevankk/Gywnw/2/ . Alerts a message when the page is refreshed.
$(window).unload(function() { alert("Unload"); });
The unload event occurs when the user navigates away from the page. The unload event is triggered when: a link to leave the page is clicked. a new URL is typed in the address bar. the forward or back buttons are used.
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 onunload attribute fires once a page has unloaded (or the browser window has been closed). onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.)
The load event works on the page, scripts, style sheets, images, and iframes. unload. The unload event works only on the page. Note that the load and unload events should also fire when you use the Back and Foward buttons to navigate.
This should work to show a confirmation when the users leaves, this is also not part of any standard.
$(window).on('beforeunload ',function() { return 'Are you sure ?'; });
I found Joseph's comment as the correct answer, So posting this answer.
Dialogs are blocked/prevented during "beforeunload" (with exception to the beforeunload prompt) and "unload" events. Can be confirmed by checking your console.
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