26

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");  
});​  
tshepang
  • 12,111
  • 21
  • 91
  • 136
Jeevan
  • 3,878
  • 6
  • 29
  • 37
  • 2
    possible duplicate of [window.onunload is not working properly in Chrome browser. Can any one help me?](http://stackoverflow.com/questions/7794301/window-onunload-is-not-working-properly-in-chrome-browser-can-any-one-help-me) – David Hedlund Apr 02 '12 at 09:26
  • 4
    dialogs are blocked/prevented during "beforeunload" (with exception to the beforeunload prompt) and "unload" events. check your console, it says it all. – Joseph Apr 02 '12 at 09:29
  • @Joseph you are right. that was the problem – Jeevan Apr 02 '12 at 10:10

7 Answers7

21

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 ?';
});
Willem D'Haeseleer
  • 19,661
  • 9
  • 66
  • 99
18

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.

Jeevan
  • 3,878
  • 6
  • 29
  • 37
6

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.

Robert
  • 3,328
  • 2
  • 24
  • 25
  • Ok, Is there anyway I can find when a tab is closed in chrome and safari? – Jeevan Apr 02 '12 at 09:29
  • Sorry i'm not sure if there is a constant way to do this, but on the duplicate question @David pointed to there was a suggestion that they could use the pagehide event http://stackoverflow.com/a/9909822/199111 – Robert Apr 02 '12 at 09:42
5

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.

jasxir
  • 808
  • 9
  • 18
2

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??

nandu
  • 779
  • 4
  • 18
  • dialogs are blocked/prevented during "beforeunload" (with exception to the beforeunload prompt) and "unload" events. check your console, it says it all Joseph was right. – Jeevan Apr 02 '12 at 10:10
1

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

nandu
  • 779
  • 4
  • 18
0

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

We should use onbeforeunload instead.

Confirm with Firefox version 47, Mac OS X

Chemical Programmer
  • 4,352
  • 4
  • 37
  • 51