View Full Version : how to call a function when user closes window

01-07-2007, 07:35 AM
I am writing a code which requires state maintainance like login. Here problem is that many of the users directly close the window without logout. This gives a problem that state maintaining variables stored in server database does not get deleted. Is there any JavaScript which could execute a function when user closes window. I have tried <body onUnload="function()"> but it will get executed when user presses 'Refresh' button. I want to catch the event of closing the window so that the session can be closed.

01-07-2007, 03:41 PM
You are no going to find a solution since different actions will cause this to fire. You can try using onbeforeunload, but that will lead to issues too.

You should not be relying on clientside code, you should be relying on your session timeout on the server to clean things up.


01-14-2007, 12:52 PM
I am new to web programming. I use Perl for server side programming. How can I use timeout. Kindly tell me more about this and also the code or atleast some referance.