Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOM Window unload event, is it reliable?

Can I rely on the window unload event to be triggered when a user closes a tab/window/browser?

Edit:

Found a list of what triggers the unload event in IE. http://msdn.microsoft.com/en-us/library/ms536973%28VS.85%29.aspx

I would like to know in which edge cases the unload event won't be triggered.

like image 945
bhseo Avatar asked Oct 20 '09 13:10

bhseo


People also ask

What triggers unload event?

onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.). Note: The onunload event is also triggered when a user reloads the page (and the onload event).

What is the difference between unload and Beforeunload?

beforeunload event – the user is leaving: we can check if the user saved the changes and ask them whether they really want to leave. unload – the user almost left, but we still can initiate some operations, such as sending out statistics.


2 Answers

You will likely want to use the "onbeforeunload" event too, it provides more control than the "onunload" event.

That said, be aware that: Opera doesn’t fire the unload event when the browser refreshes the page, or uses the back/forward buttons to browse off of the page. What’s worse, Opera never fires the onbeforeunload event. Thus if you are supporting Opera - be aware of these issues.

like image 189
scunliffe Avatar answered Sep 18 '22 23:09

scunliffe


Not in all circumstances no. The browser could crash etc. keeping the event from firing. The user could also use add-ons like Grease Monkey to prevent the event from firing also.

like image 42
kemiller2002 Avatar answered Sep 18 '22 23:09

kemiller2002