nauman73
10-10-2003, 04:26 AM
Hi
I am trying to capture the browser close (or closing) event so that I can end the user session whenever the user closes his/her browser window. I have tried 3 different solutions but I still have problems. I am giving the approaches that I have tried.
1. I have tried calling my logout code on the 'onunload' event of the current page. Although this event works when user closes his/her browser window but it has one major problem.
If the user presses refresh button on the browser, this event is fired and the logout code is executed. This is not what I wanted.
2. Another possible solution that I found from web was to execute my logout script on the 'onunload' event of window object. The script declaration is given below.
<script for="WINDOW" event="onunload">
//my logout javascript here
</script>
Again, the script works when I close the browser window but it has the same problem. Even this event gets fired on refresh.
3. I tried to place a check in my javascript by writing this piece of code.
if (window.closed)
{
// do the logout
}
I was expecting that 'window.closed' will return 'true' when the user closes borwser window and I would be able to execute my logout code only when I get 'true'. I thought that 'window.closed' will return false on refresh and the logout code will not be executed.
If all wishes could come true... :) 'Window.closed' returns false even if I close the browser window. So this solution disables the logout alltogether :)
Any ideas? Will appreciate any help.
Thanks.
Nauman
I am trying to capture the browser close (or closing) event so that I can end the user session whenever the user closes his/her browser window. I have tried 3 different solutions but I still have problems. I am giving the approaches that I have tried.
1. I have tried calling my logout code on the 'onunload' event of the current page. Although this event works when user closes his/her browser window but it has one major problem.
If the user presses refresh button on the browser, this event is fired and the logout code is executed. This is not what I wanted.
2. Another possible solution that I found from web was to execute my logout script on the 'onunload' event of window object. The script declaration is given below.
<script for="WINDOW" event="onunload">
//my logout javascript here
</script>
Again, the script works when I close the browser window but it has the same problem. Even this event gets fired on refresh.
3. I tried to place a check in my javascript by writing this piece of code.
if (window.closed)
{
// do the logout
}
I was expecting that 'window.closed' will return 'true' when the user closes borwser window and I would be able to execute my logout code only when I get 'true'. I thought that 'window.closed' will return false on refresh and the logout code will not be executed.
If all wishes could come true... :) 'Window.closed' returns false even if I close the browser window. So this solution disables the logout alltogether :)
Any ideas? Will appreciate any help.
Thanks.
Nauman