Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  72] [ 2]  / answers: 1 / hits: 43130  / 14 Years ago, tue, december 7, 2010, 12:00:00

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?


More From » jquery

 Answers
37

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.


[#94697] Sunday, December 5, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devlin

Total Points: 474
Total Questions: 113
Total Answers: 100

Location: Sweden
Member since Fri, Apr 16, 2021
3 Years ago
devlin questions
Tue, Apr 27, 21, 00:00, 3 Years ago
Sat, Oct 31, 20, 00:00, 4 Years ago
Fri, Aug 28, 20, 00:00, 4 Years ago
;