views:

2473

answers:

2

Hi!

How I can capture event, close browser window, in jQuery or javascript ?

+2  A: 

http://docs.jquery.com/Events/unload#fn

jQuery:

$(window).unload( function () { alert("Bye now!"); } );

or javascript:

window.onunload = function(){alert("Bye now!");}
ToRrEs
this event work and when I do refresh, but i need, only when I close the browser!
Alexander Corotchi
@Alexander Corotchi: Why do you want to know when the user closes the browser?
Steve Harrison
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.
Alexander Corotchi
+1  A: 

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
But in Ie7 it doesn't work ?!
Alexander Corotchi
it does not work in many browsers. but that is the only event for the closing of a browser window
Jonathan Fingland