| Author |
Trapping Browser Close Event
|
Jason Menard
Sheriff
Joined: Nov 09, 2000
Posts: 6450
|
|
Is there any crossbrowser way to fire off a JavaScript function when the user closes the browser? I've tried onUnload but that fires off even when other links are clicked. I only want it to fire when the browser is closed. This doesn't work: In the above code, shutDown() fires when I click "Some Link", which is not the desired behavior. Any help is much appreciated.
|
Jason's Blog
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
There is no REAL solution to this common problem. The refresh button sets it off, the back button sets it off, links set it off. Now there was a way to do it in just IE where it could detect mouse postion off the page, but that did not account for alt-f4 combo. The only way to do it with just links is to add an onclick atribute to everything telling the event not to fire. A script can be written that would add all of the onclicks to the links, but this does not account for the back button nor the refresh button. Man did I just ramble or what! Eric
|
 |
Jason Menard
Sheriff
Joined: Nov 09, 2000
Posts: 6450
|
|
Thanks Eric. Yeah, I ended up doing the whole onclick thing. Here's what I did just in case anyone else is wondering. NOTE: Cutting and pasting the above code won't work. I had to replace the letter L's in "onLoad", "onUnload", and "onClick" in the above code with the number 1 in order for UBB to accept my post. [ February 04, 2004: Message edited by: Jason Menard ]
|
 |
Sonny Gill
Ranch Hand
Joined: Feb 02, 2002
Posts: 1211
|
|
I was trying to work a way to do that too. Here I tried to discuss a couple of approaches to it I was think about - http://www.coderanch.com/t/357828/Servlets/java/Java-gurus-please-Detecting-browser Now I am considering having an invisible applet on the page, which makes connection to the server every few seconds, and when the server-side monitor stops receiving a connection from a particular client, it assumes that the browser was closed. In the link above, I was thinking of putting my webpage in a frameset, and have another page in the other(almost invisible) frame that keeps refreshing itself, to register the client with the server. The problem with that is, I am worried using frames might create unforeseen problems, and if a user right clicks and opens the link in a new window, that window has no frames, and hence no self-refreshing page. Would love to get comments on any of these ways of doing it.
|
The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
|
 |
Yuriy Fuksenko
Ranch Hand
Joined: Feb 02, 2001
Posts: 413
|
|
You probably could use hidden IFRAME to keep updating. But personally I would prefer to have a nice session timeout time About closing - I don't remember where I have seen it, but there was a nice trick to submit a "last message" to a server. It was using src on image tag. Something like <img name="lastMsg" style="visibility:hidden"> function on_Unload() { document.images.lastMsg.src="www.myurl.com?var=text1&var2=text2&var3=text3"; }
|
 |
Sonny Gill
Ranch Hand
Joined: Feb 02, 2002
Posts: 1211
|
|
Originally posted by Yuriy Fuksenko: [QB][/QB]
thanks mate, that might come in handy one of these days.
|
 |
 |
|
|
subject: Trapping Browser Close Event
|
|
|