Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is jQuery unload not working in chrome and safari?

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");   });​   
like image 288
Jeevan Avatar asked Apr 02 '12 09:04

Jeevan


People also ask

What is unload in jquery?

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.

What is the difference between Onbeforeunload and Onunload?

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.

What does Onunload do in Javascript?

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.)

What is load and unload in Javascript?

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.


2 Answers

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 ?'; }); 
like image 60
Willem D'Haeseleer Avatar answered Oct 10 '22 13:10

Willem D'Haeseleer


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.

like image 40
Jeevan Avatar answered Oct 10 '22 12:10

Jeevan