How I can capture event, close browser window, in jQuery or javascript ?
4 Answers
http://docs.jquery.com/Events/unload#fn
jQuery:
$(window).unload( function () { alert("Bye now!"); } ); or javascript:
window.onunload = function(){alert("Bye now!");} 5 Comments
AlexC
this event work and when I do refresh, but i need, only when I close the browser!
Steve Harrison
@Alexander Corotchi: Why do you want to know when the user closes the browser?
AlexC
In 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.
Pavel Hodek
This is not good enough. The only working solution is Browser window close event
e-info128
Fail, when page change url call unload event, but no only close. unload != close.
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)
3 Comments
Jonathan Fingland
it does not work in many browsers. but that is the only event for the closing of a browser window
Pavel Hodek
This is not good enough. The only working solution is Browser window close event
mansour lotfi
Sorry, the page /en-US/DOM/window.onclose could not be found.
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?
1 Comment
Stijn de Witt
Spamming your answer across the comments and then it turns out to be a link-only answer....
Men, use this:
if(myWindow.closed){ callback(); return; } 1 Comment
AlliterativeAlice
This isn't "capturing"