Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to detect browser closing/navigation to other page and do logout

I am writing an application in GWT and I need to detect when a user navigates away from my application or when he closes the browser window (onUnload event) and do a logout (session invalidation and few other cleanup tasks). The logout action is performed by a servlet.

I am currently doing this by hooking into the onUnload() event and opening a new window pointed to the logout servlet.

Is there a better way to do this? Any other suggestions are welcome.

like image 247
Saravanan M Avatar asked May 20 '09 15:05

Saravanan M


3 Answers

Looks like GWT does have an event for exactly this.

ClosingEvent.

Looks like you need to implement a ClosingHandler

like image 136
Carnell Avatar answered Nov 08 '22 18:11

Carnell


Why not just make a very short lived session cookie that is reset with each page load, then add a tracking cookie. When the user returns you notice the tracking cookie but no session cookie. Expire the session and clear everything up at that point.

Pop up blockers will prevent your session clean up when it blocks the onUnload window open, because this is something spammers use.

like image 2
Bjorn Avatar answered Nov 08 '22 19:11

Bjorn


This is how the closing event works:

Window.addWindowClosingHandler(new Window.ClosingHandler()
{
 @Override
 public void onWindowClosing(ClosingEvent event)
 {
  event.setMessage("Are you sure?");
 }
});

Then GWT gives the user a chance to say yes or no. Of course you can also add a test in there to see if they've got any unsaved data or whatever you want. Not setting the message or setting it to null doesn't do anything.

like image 1
Bill Lyons Avatar answered Nov 08 '22 18:11

Bill Lyons