• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to catch browser close event?

 
Manoj Amrutkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manoj Amrutkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can you provide some code please?

Thanks
Manoj
 
Ryan Asleson
Author
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something like this for the onunload event:

 
Manoj Amrutkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic