Jeevan
Jeevan

Reputation: 3938

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");  
});​  

Upvotes: 26

Views: 50358

Answers (7)

Chemical Programmer
Chemical Programmer

Reputation: 4520

"refresh" action in the Firefox does not fire the unload event.

We should use onbeforeunload instead.

Confirm with Firefox version 47, Mac OS X

Upvotes: 0

Willem D'Haeseleer
Willem D'Haeseleer

Reputation: 20180

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 ?';
});

Upvotes: 21

jasxir
jasxir

Reputation: 918

jQuery's unload works well in chrome too, with the exception that Chrome doesn't allow alerts within it. I've used it to set cookies. And if it works with Chrome hope it works in Safari too.

Upvotes: 5

Jeevan
Jeevan

Reputation: 3938

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.

Upvotes: 18

nandu
nandu

Reputation: 779

you can use onfocusout on the body .. but i wouldn't recommend if you are trying to use something like an alert, on this operation, asking the user not to leave your page ..

Upvotes: 1

nandu
nandu

Reputation: 779

the unload function of jquery has some problem with browsers..refer the following link http://bugs.jquery.com/ticket/5538

can you elaborate on the problem so that we can find some work around??

Upvotes: 2

Robert
Robert

Reputation: 3408

This is because the unload event is not part of any standard

https://developer.mozilla.org/en/DOM/window.onunload

check the bottom of the page i just linked to.

Upvotes: 6

Related Questions