GWT WindowClosingHandler射击刷新浏览器太 [英] GWT WindowClosingHandler firing on Browser refresh too

查看:147
本文介绍了GWT WindowClosingHandler射击刷新浏览器太的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我面临的code的一个问题下面我写来检测浏览器的关闭事件,使用户注销

Here I am facing an issue with the code below I wrote to detect the browser's close event to make user logged out from website.

但遗憾的是在刷新过此事件:(

But unfortunately this event fires on Refresh too :(.

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

是的,我经历了
  GWT检测是closeHandler 浏览器刷新

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

But I didn't find any positive results there.

根据GWT <一个href=\"http://google-web-toolkit.google$c$c.com/svn/javadoc/2.0/com/google/gwt/user/client/Window.ClosingEvent.html\"相对=nofollow> Window.ClosingEvent 类的API:

As per GWT Window.ClosingEvent class api:

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

但我还是希望大家能够检测刷新浏览器

任何一个可以给这方面的暗示?

Can any one give a hint regarding this?

推荐答案

有IMPOSIBLE知道,如果 Window.ClosingEvent 是被称为刷新或关闭浏览器。但是,如果你的问题是只有在浏览器已经关闭进行检测,就可以使用会话cookie。

It is imposible to know if the Window.ClosingEvent is being called to refresh or to close the browser. But, if your problem is only to detect if the browser has been closed, you can use session cookies.

这里有一个实用工具类,它可以帮助你。只要打电话给下面的线条在 onModuleLoad 来测试。

Here is one utility class that can help you. Just call the lines below in your onModuleLoad to test.

测试线

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

这篇关于GWT WindowClosingHandler射击刷新浏览器太的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆