3

I'm a bit confused, why this

window.onbeforeunload = function () {
socket.emit('foo')
}

does nothing, while this

$(window).on("beforeunload", function () {
socket.emit('foo');
});

Makes the job done on chrome 42.0.2311.90 m What is the difference?

And how reliable jQuery version is?
I want to use onbeforeunload to distinguish disconnect event while user leaves the app from disconnect event while something terribly failed in socket connection on the server or user side.

Max Yari
  • 3,617
  • 5
  • 32
  • 56
  • 1
    For the javascript version you need a `return`, as laid out in [this answer](http://stackoverflow.com/questions/7255649/window-onbeforeunload-not-working). – Brett DeWoody Apr 16 '15 at 16:41
  • @BrettDeWoody ah good to know, thx, what about their reliability? Did JQuery just wraps `window.addEventListener('onbeforeunload',...)` and therefore they are identical? – Max Yari Apr 16 '15 at 16:49
  • FYI, you cannot rely on the `beforeunload` always getting called. While it may normally be called, there can be some circumstances where it does not get called (power shutdown, laptop/tablet goes to sleep, browser process crashes, etc...). So, you do have to know how your server is going to behave if it does not get called and have some sort of backstop design for that. – jfriend00 Apr 16 '15 at 21:01
  • Yep, it seems that i searched poorly, that questions is realy answers mine. – Max Yari Apr 17 '15 at 21:39

0 Answers0