GWT detects browser update in CloseHandler

I have a GWT application and I want to run some code when the user leaves the application to force log out and delete any data, etc.

To do this, I use CloseHandler and register it with Window.addCloseHandler.

I noticed that when the update button is clicked, the onClose method is launched, but I could not distinguish this event from closing when the user closed the browser. If this is an update, I do not want to do logout, etc., I only want to do this when the user closes the browser / tab or moves away from the site.

Does anyone know how I can do this?

+3
source share
3 answers

It is impossible to distinguish β€œclose” from β€œupdate”. But you can set a cookie that contains the last time you called CloseHandler, and check when loading the module if this time is old enough to clear the information before displaying the page.

You can do this with the following utility class ( BrowserCloseDetector ). Here is an example of using it on onModuleLoad .

Test Lines :

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

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; } } 
+1
source

You tried

<BODY onUnload = "scriptname">

in your gwt hosting / running html file?

I think that if in the hosting file you defined a "hash" (i.e. the javascript pseudo-hash) and then accessed the "hash" in GWT through the Dictionary class, you could update the values ​​in this hash as the user progresses through the gwt application. This means that your programming style will require you to register milestones in the progress of the user on this map.

When the user closes the browser page, the onunload script will be launched on the html launch page. This means that the script will access the map to find out what needs to be updated on the server, or what other URL to run.

0
source

I am also full if someone got a solution (only GWT / java). Maybe we can do this with a HistoryListerner? 1 - set the flag for the current view page. 2 - when the ClosingHandler event triggers a timeout on the server side (for example, 10 seconds) 3 - if during this time you received a massage from HistoryListerner with the same last flag, which was just an update. disconnect if timer runs out ...

This is not a good solution, but I think it is easy to do ... If anyone has the best ...

0
source

All Articles