本文介绍了GWT在CloseHandler中检测浏览器刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GWT应用程序,当用户离开应用程序强制注销并删除任何数据时,我想运行一些代码。

为此,我我使用了一个CloseHandler并使用Window.addCloseHandler注册它。



我注意到当单击刷新按钮时,onClose方法运行,但我一直无法区分这来自用户关闭浏览器的关闭事件。如果是刷新,我不想做注销等,我只想在用户关闭浏览器/选项卡或从网站导航时执行此操作。



有人知道我该怎么做吗?

解决方案

没有办法区分'close'和'refresh'。但是,您可以设置一个保存最后一次CloseHandler调用时间的cookie,并在加载模块时检查,如果此时间足够长以在显示页面之前清除信息。



BrowserCloseDetector )来完成此操作。以下是在 onModuleLoad 中使用它的示例。



测试行

  @Override 
public void onModuleLoad(){
if(BrowserCloseDetector.get()。wasClosed() ){
GWT.log(浏览器已关闭。);
}
else {
GWT.log(刷新或从另一个页面返回。);


code

$ b

实用程序类

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

public class BrowserCloseDetector {
private static final String COOKIE =detector;
私有静态BrowserCloseDetector实例;

私有BrowserCloseDetector(){
Window.ClientHandler(){
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;
}
}


I have a GWT application and I want to run some code when the user leaves the application to force a logout and remove any data etc.

To do this I am using a CloseHandler and registering it using Window.addCloseHandler.

I have noticed that when the refresh button is clicked the onClose method is run but I have been unable to differentiate this event from a close where the user has closed the browser. If it is a refresh I do not want to do the logout etc, I only want to do this when the user closes the browser/tab or navigates away from the site.

Does anybody know how I can do this?

解决方案

There is no way to differentiate the 'close' from 'refresh'. But, you can set a cookie that holds the last CloseHandler call time and check, when loading the module, if this time is old enough to clean the information before showing the page.

You can do that with the folowing utility class (BrowserCloseDetector). Here is an example using it on the onModuleLoad.

The test lines:

@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;
    }
}

这篇关于GWT在CloseHandler中检测浏览器刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 00:35