File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes Trapping Browser Close Event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Trapping Browser Close Event" Watch "Trapping Browser Close Event" New topic

Trapping Browser Close Event

Jason Menard

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.
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
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!
Jason Menard

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 -
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
Sonny Gill LinkedIn
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()
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Originally posted by Yuriy Fuksenko:

thanks mate, that might come in handy one of these days.
I agree. Here's the link:
subject: Trapping Browser Close Event
It's not a secret anymore!