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

4

8

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.

Mont answered 20/5, 2009 at 15:2 Comment(3)
Duplicate of this question. #875880Surveyor
That question is not a duplicate of this one.Quadrangular
The answer does answer this question though - you can't tell when the user has stopped using your site. The closest you can do is to tell when the user hasn't sent a new request to the site for a given period of time.Hage
B
5

Looks like GWT does have an event for exactly this.

ClosingEvent.

Looks like you need to implement a ClosingHandler

Beauty answered 20/5, 2009 at 15:40 Comment(4)
You should be able to send one last logout request to your server in your handler.Beauty
I just ran a quick test to create a ClosingHandler and registered it. I made a simple one that just displays an alert. The code is getting fired when the tab/window is closed as well as when I browse to another page outside of the app.Beauty
I am aware of this WindowCloseListener (equivalent of ClosingEvent in GWT 1.5), but was in the assumption that since a RequestCallback object is mandatory to make a Request, it may cause some problem on the callback events. But I tried with a RequestCallback doing nothing on the onResponseReceived event, and it is working fine. Thanks for the response.Mont
Answers like this are exactly why only linking to information shouldn't be allowed. Both of those are broken.Shipentine
Q
2

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.

Quadrangular answered 20/5, 2009 at 15:4 Comment(0)
M
1

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.

Mudlark answered 9/11, 2010 at 22:56 Comment(0)
P
0

The way to do that is to use the Window.addWindowClosingHandler like @Carnell and @BillLyons said. But I use an additional technique to detect if the browser has been closed or if the page is being visited again (by refresh or back navigation).

Following there's an utility class that can help you. Just call the lines below in your onModuleLoad to test.

The use example:

@Override
public void onModuleLoad() {
    if (BrowserCloseDetector.get().wasClosed()) {
        GWT.log("Browser was closed.");
    }
    else {
        GWT.log("Refreshing or returning from another page.");
    }
}

The utility class:

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;

public class BrowserCloseDetector {
    private static final String COOKIE = "detector";
    private static BrowserCloseDetector instance;

    private BrowserCloseDetector() {
        Window.addWindowClosingHandler(new Window.ClosingHandler() {
            public void onWindowClosing(Window.ClosingEvent closingEvent) {
                Cookies.setCookie(COOKIE, "");
            }
        });
    }

    public static BrowserCloseDetector get() {
        return (instance == null) ? instance = new BrowserCloseDetector() : instance;
    }

    public boolean wasClosed() {
        return Cookies.getCookie(COOKIE) == null;
    }
}
Pasto answered 21/2, 2013 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.