23

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

1

4 Answers 4

16

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

jQuery:

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

or javascript:

window.onunload = function(){alert("Bye now!");} 
Sign up to request clarification or add additional context in comments.

5 Comments

this event work and when I do refresh, but i need, only when I close the browser!
@Alexander Corotchi: Why do you want to know when the user closes the browser?
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.
This is not good enough. The only working solution is Browser window close event
Fail, when page change url call unload event, but no only close. unload != close.
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)

3 Comments

it does not work in many browsers. but that is the only event for the closing of a browser window
This is not good enough. The only working solution is Browser window close event
Sorry, the page /en-US/DOM/window.onclose could not be found.
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?

1 Comment

Spamming your answer across the comments and then it turns out to be a link-only answer....
-1

Men, use this:

if(myWindow.closed){ callback(); return; } 

1 Comment

This isn't "capturing"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.