View Full Version : searching for a trigger like "onWindowClose"

09-01-2005, 01:02 PM
Hey there,

i had to create an onlineview for special data. Therefore the customer has to login to see only his own data. The login is kept in a database with several informations.

On the mainscreen there is a logout-button, so the customer can destroy the current session and nobody can missuse his login.
But what, if the customer only closes the window ? The session is still running and there is the possibility of missuse.
Hence i wanted to add the logout in a function which is called, if the page windows is closed. I would need something like an "onWindowClose"-trigger.

Does anyone has an idea how to solve that ?

thanks in advance

STEFAN aka whynot

09-01-2005, 01:48 PM
It'd be a lot safer to simply have a short session timeout with a cookie-based session limiter.

For example, when I'm using Natwest banking online, if I close the window, the session cookie is no longer present and I need to log in again. If I leave it inactive for 3 minutes, I need to log in again.


09-01-2005, 02:29 PM
I'd advise using both.
onunload() is what you want.

09-01-2005, 02:55 PM
I'm with cr3 on this one Twey, besides onunload() is invalid and will do nothing. If you want the double whammy of a server side cookie (what cr3 is suggesting) and a javascript approach, the correct use of onunload is:


It can be used identically to the many and varied syntactical ways onload is.

09-01-2005, 02:58 PM
Oh, I just use () to indicate a function. Sorry if I caused confusion.

09-02-2005, 01:17 PM
There are problems with using the unload event to log a user out. There is the obvious reason: scripting is unavailable. There are less obvious reasons, too:

The user might not be connected to the network.
Script errors (your own, or introduced by advertising, perhaps) may prevent the code executing.
The browser might crash.
You also need to consider that the event doesn't fire just because the browser is closed. Here's a list from Microsoft:

Close the current browser window.
Navigate to another location by entering a new address or selecting a Favorite.
Click the Back, Forward, Refresh, or Home button.
Click on an anchor that refers the browser to another Web page.
Invoke the anchor click method.
Invoke the document write method.
Invoke the document open method.
Invoke the document close method.
Invoke the window close method.
Invoke the window open method, providing the possible value _self for the window name.
Invoke the window navigate or NavigateAndFind method.
Invoke the location replace method.
Invoke the location reload method.
Specify a new value for the location href property.
Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the submit method.
It should be noted that not all of these causes will be implemented by other browsers. For instance, Opera doesn't fire the unload event at all when a tab or the browser instance is closed (something else that renders use of the event pointless).

I would suggest just implementing a server-side timeout mechanism. If well-written, there is little risk in having idle sessions.