Calling Logout on closing my browser window

Posted on 2003-03-20
Medium Priority
Last Modified: 2012-08-14
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 ..!!!
Question by:agsingh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 18

Expert Comment

ID: 8178945
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.


Author Comment

ID: 8180673
cannot use onunload(). As it is called even when i refresh my page.

Expert Comment

ID: 8201618
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>


Independent Software Vendors: 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!


Author Comment

ID: 8212504
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...!!!

Accepted Solution

Jitu earned 200 total points
ID: 8215433
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.

Expert Comment

ID: 9058567
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.
LVL 15

Expert Comment

ID: 9730562
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question