52

I want to post an message to the server when user navigate off from the current page, I am using .unload right now but the result is unreliable, even in its document is said true:

The exact handling of the unload event has varied from version to version of browsers. For example, some versions of Firefox trigger the event when a link is followed, but not when the window is closed. In practical usage, behavior should be tested on all supported browsers, and contrasted with the proprietary beforeunload event.

Should I use beforeunload event? Is it reliable?

Bin Chen
  • 61,507
  • 53
  • 142
  • 183

1 Answers1

68

Yes, beforeunload is more reliable, but be sure to assign it directly (not bound through jQuery), like this:

window.onbeforeunload = function() { /* do stuff */ };

The unload event itself wasn't meant for work to be done, only cleanup of objects...as garbage collectors get better and better, there's less reason for the browser to even fire the unload event.

Also be aware that for your specific case you'd have to make a synchronous request to the server...otherwise the browser still won't wait for the AJAX call to complete.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 1
    Does window.onbeforeunload fire on all the browsers (say IE6)? – Kangkan Dec 07 '10 at 12:34
  • 1
    @Kangkan - yup, though some more nefarious ad blocks will prevent this...they are pretty rare though. – Nick Craver Dec 07 '10 at 12:35
  • it should be mentioned that you can return a string value out of the eventhandler, which is displayed to the user. – jAndy Dec 07 '10 at 12:37
  • @jAndy - yup, in this case though he has a specific use, read the question closer ;) – Nick Craver Dec 07 '10 at 12:39
  • 16
    @Nick Craver - Why shouldn't you bind it through jQuery? Is it less reliable or something? – kmb64 Jun 10 '12 at 07:11
  • @nz_karl - yup...that may have changed recently, but at the time of this answer it didn't work correctly in all browsers/situations when done via jQuery. – Nick Craver Jun 10 '12 at 10:16
  • Just in case anyone wants some more info [here](https://developer.mozilla.org/en-US/docs/Web/API/window.onbeforeunload) is the MDN documentation that I found while googling my problem. Maybe it's worth noting that W3C standard recommends to attach the event with addEventLisTener, but it seems to have some legacy issues... – Manuel Navarro Sep 16 '13 at 16:34
  • 3
    @NickCraver: Not via jQuery? Some kind of reference would be good, otherwise this is just spreading FUD – Marc-André Lafortune Dec 17 '14 at 21:20
  • 4
    The jQuery documentation itself says to use native JavaScript in preference to jQuery if support for the native function is good cross browser. One reason would be that the native function is built into the browser and therefore much faster than the jQuery library function. – Chaoley Mar 26 '15 at 14:47
  • Why do you need to collect garbage on page close? Browser releases all the page memory anyway, that's why it's called `unload` – Finesse May 22 '19 at 02:18