How I can capture event, close browser window, in jQuery or javascript ?
Asked
Active
Viewed 1.2e+01k times
23
-
2possible duplicate of [Browser window close event](http://stackoverflow.com/questions/1631959/browser-window-close-event) – Danubian Sailor Mar 28 '14 at 07:32
4 Answers
16
http://docs.jquery.com/Events/unload#fn
jQuery:
$(window).unload( function () { alert("Bye now!"); } );
or javascript:
window.onunload = function(){alert("Bye now!");}

jeroen.verhoest
- 5,173
- 2
- 27
- 27
-
1this event work and when I do refresh, but i need, only when I close the browser! – AlexC Jun 22 '09 at 07:52
-
1@Alexander Corotchi: Why do you want to know when the user closes the browser? – Steve Harrison Jun 22 '09 at 08:19
-
1In my application can work more users, And when 1 user close browser a need to execute a function for exclude this user from this application. – AlexC Jun 22 '09 at 08:51
-
This is not good enough. The only working solution is [Browser window close event](http://stackoverflow.com/questions/1631959/browser-window-close-event#1632004) – Pavel Hodek May 07 '12 at 10:53
-
1Fail, when page change url call unload event, but no only close. unload != close. – e-info128 Oct 25 '12 at 18:08
9
You're looking for the onclose event.
see: https://developer.mozilla.org/en/DOM/window.onclose
note that not all browsers support this (for example firefox 2)

Jonathan Fingland
- 56,385
- 11
- 85
- 79
-
3it does not work in many browsers. but that is the only event for the closing of a browser window – Jonathan Fingland Jun 22 '09 at 08:30
-
2This is not good enough. The only working solution is [Browser window close event](http://stackoverflow.com/questions/1631959/browser-window-close-event#1632004) – Pavel Hodek May 07 '12 at 10:55
-
3
Events onunload or onbeforeunload you can't use directly - they do not differ between window close, page refresh, form submit, link click or url change.
The only working solution is How to capture the browser window close event?

Community
- 1
- 1

Pavel Hodek
- 14,319
- 3
- 32
- 37
-
Spamming your answer across the comments and then it turns out to be a link-only answer.... – Stijn de Witt Dec 11 '17 at 14:53