File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes How to catch browser close event? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to catch browser close event?" Watch "How to catch browser close event?" New topic
Author

How to catch browser close event?

Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
Hi,
Well it's amazing to know that there is no way i can track the event of browser closing(IE-6, and when i say browser close it's either clicking on X button or Alt+F4 only {both of them together} and not page refresh of back/fwd buttons), and this statement comes after lot of R&D.
But still i feel there must be something by which javascript can do it?

Is there any one who can provide some code for that thing?

Thanks
Manoj
Ryan Asleson
Author
Ranch Hand

Joined: Sep 15, 2005
Posts: 39
You can try capturing the window.onclose event or the document.onunload event. I've tried these before and have somewhat uneven results, so try it in your environment.


Ryan Asleson<br />Co-Author, <a href="http://www.amazon.com/Foundations-Ajax-Foundation-Ryan-Asleson/dp/1590595823/sr=1-1/qid=1158926537/ref=pd_bbs_1/002-7412154-2924860?ie=UTF8&s=books" target="_blank" rel="nofollow">Foundations of Ajax</a> and <a href="http://www.amazon.com/Pro-Ajax-Java-Frameworks/dp/1590596773/ref=sr_11_1/002-7412154-2924860?ie=UTF8" target="_blank" rel="nofollow">Pro Ajax and Java Frameworks</a><br /><a href="http://taconite.sf.net" target="_blank" rel="nofollow">Taconite</a> Lead Developer
Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
Hi,
Can you provide some code please?

Thanks
Manoj
Ryan Asleson
Author
Ranch Hand

Joined: Sep 15, 2005
Posts: 39
Try something like this for the onunload event:

Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
hi,
thanks, but as i allready mentioned in my first post that unload or onbeforeunload gets called on refresh and back/fwd buttons.
i have a chat application in which suppose user closes the browser without proper logout his session has to be expired and he will be removed from chat room's list(which is maintained in context, and that i will do with DWR in javascript).
but for that i need to catch that event isn't it?

Thanks
Manoj
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
There is no way to really do it as you mentioned. You need to rely on the server's session to expire and dump the user. Not pretty, but that is all you really have.

Personally you can do some sort of think without session. If you do not get a response from the user (Ajax request to check for new data) than you know they are gone and dump them.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to catch browser close event?