【问题标题】:GWT WindowClosingHandler firing on Browser refresh tooGWT WindowClosingHandler 也会在浏览器刷新时触发
【发布时间】:2013-02-15 15:39:41
【问题描述】:

这里我遇到了以下代码的问题,我编写了以下代码来检测browser's close event 以使用户logged out 来自网站。

但不幸的是,此事件也会在 Refresh 上触发 :(.

Window.addWindowClosingHandler(new Window.ClosingHandler() {
    public void onWindowClosing(Window.ClosingEvent closingEvent) {
        closingEvent.setMessage("Do you really want to close the page?");
    }
});

是的,我通过了 GWT detect browser refresh in CloseHandler

但我没有发现任何积极的结果。

根据 GWT Window.ClosingEvent class api:

Fired just before the browser window closes or navigates to a different site.

但我仍然希望我们能检测到browser refresh

任何人都可以对此提供提示吗?

【问题讨论】:

    标签: events gwt user-interface browser


    【解决方案1】:

    无法知道是调用Window.ClosingEvent 来刷新还是关闭浏览器。但是,如果您的问题只是检测浏览器是否已关闭,您可以使用会话 cookie。

    这是一个可以帮助您的实用程序类。只需在您的onModuleLoad 中调用以下行进行测试。

    测试线

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

    实用程序类

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

    【讨论】:

    • @TheSureshAtta 代码确实有效。也许不是你想要的,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2019-06-23
    • 2015-02-18
    • 2020-11-06
    • 1970-01-01
    相关资源
    最近更新 更多