• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 788
  • Last Modified:

Calling Logout on closing my browser window

want to call my logout servlet if someone hits browser close button.
I tried all javascript combinations with onUnLoad() window.close() etc but nothing wrks as these events gets fired even on refresh and changing the locationof the url.

I was giving up when i saw the implementation in citibanks website. If anyone has account with citibank then u can check it out.
I tried to look into the view source but coulnd't make out anything..

any ideas will be highly appreciated ..!!!
1 Solution
You can use onUnload.....

function forceLogoff(){
window.open("/servlet/LogoutServlet","LogoutWin", "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=500,height=150");

and put this in yout body tag
<BODY onUnload="forceLogoff()">

It will pop up a logoff page.

agsinghAuthor Commented:
cannot use onunload(). As it is called even when i refresh my page.
Infact onUnload will be called even when u r going from one page to another !!

Here is a guess :
I haven't used Citibank website, but my guess is that it actually does not log you out when you close the window. Instead when u try returning to the website, it recognises that you are not using a valid previous page to come to the requested page, and it logs you out at that point.

In a typical MVC model :
1. The only way to go from one page to another is via a Controller Servlet.
2. Any direct access to a page (jsp) is denied. Any attempt for direct access, sends the user to login page.


In any case , here is a not-very-nice way to achieve what you want via javascript, assuming there was one link on my page. This gives u a general idea :

<body onLoad="closing=true" onUnload="if (closing) window.open('thanksForVisiting.html','newwin');">

<a href="nextpage.htm" onClick="closing=false">Click to go to another page..</a>


Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

agsinghAuthor Commented:
Let me reframe my question. I want to do some action when i am closing my browser window(like open a win)
The code that u have written works fine, but the only problem is as i mentioned earlier when i try to refresh my page same is executed. How can i prevent this...!!!
If u ask me, user should indeed get logged out if he tries to refresh the page. This is the behavior in citibank site too... infact this is what they have done :

1. Removed all toolbar buttons from the window. So user cannot hit refresh.
2. Even the refresh available via right click popup menu is disabled i think.

ICICI Bank logs you out if you hit refresh, try to move across the website by clickin Browser Back/Forward buttons etc. The only valid way to move across pages for them is to hit the Previous/Next etc. buttons and link provided on the pages.
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Jitu's comment as answer.

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now