区分浏览器选项卡关闭和刷新(以检测应用程序的多个实例) [英] Differentiate browser tab close and refresh (to detect multiple instances of application)

查看:95
本文介绍了区分浏览器选项卡关闭和刷新(以检测应用程序的多个实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何区分浏览器选项卡关闭和刷新功能。



截至目前,窗口刷新和关闭事件没有不同的事件。 b


我的要求是检查已经登录或不在任何选项卡中的天气用户,以便我不允许他在任何其他选项卡中加载我的应用程序。


GWT(java)

  private void registerWindowCloseEvent(){
Window.addCloseHandler(new CloseHandler< Window>(){
@Override
public void onClose(CloseEvent< Window>事件){
//在close
上做些事情}
});

$ b $ / code>

JavaScript / Jquery:

  window.onbeforeunload = function(e){
//在close b $ b};

上述事件对事件刷新和关闭都有效。有什么办法可以区分。

解决方案区分浏览器选项卡关闭和刷新功能确实很痛苦,因为我们没有两个事件可以知道哪个事件被触发。



但总是有一些要求:)

我在做的是在负载中设置一个 cookie ,如果发现 cookie 并在浏览器关闭事件中删除cookie。



因此,除非他关闭了活动选项卡(登录选项卡),否则该cookie仍然存在,如果他试图打开另一个选项卡,那么已经激活的对话框出现了。



注意:解决方案提供了 Cookies帮助。



这里是GWT的onModule()/和 onload / document.ready()相同 java script / Jquery

  @Override 
public void onModuleLoad(){
if(already_in_browser。
equalsIgnoreCase(Cookies.getCookie(already_in_browser))){
showAlreadyTabActiveDialog();
return;
} else {
setLoggedincookie();
}

private void setLoggedincookie(){
isLoggedintab = true; //这是一个全局变量
registerWindowCloseEvent();
com.google.gwt.user.client.Cookies。
setCookie(already_in_browser,already_in_browser);


$ b private void showAlreadyTabActiveDialog(){
alert(您已经在另一个选项卡中激活);
registerWindowCloseEvent();


$ b / **这个事件是在javascript中的onbeforeunload中加载的
$ b private void registerWindowCloseEvent(){
Window.addCloseHandler(new CloseHandler< ();窗口>(){
@Override
public void onClose(CloseEvent< Window> event){
if(isLoggedintab){
Cookies.removeCookie(already_in_browser);
}
}
});




$ b $ p
$ b

让我知道如果您发现任何错误或循环漏洞如果有人提供了一个解决方案,而不使用 cookies ,那么我会很高兴, / code>。


How can Differentiating browser tab close and refresh functionality.

As of now window refresh and close event doesn't have different events.

My requirement is to checking weather user already logged in or not in any of tabs,So that I wont allow him to load my app in any other tabs.

In GWT (java)

private void registerWindowCloseEvent() {
                 Window.addCloseHandler(new CloseHandler<Window>() {
                        @Override
                        public void onClose(CloseEvent<Window> event) { 
                            // do something on close
                        }
                    });

            }

in JavaScript/Jquery:

 window.onbeforeunload = function(e) {
        //   do something on close
    };

The above events are firing for both the events refresh and close..is there any way to differentiate.

解决方案

Differentiating browser tab close and refresh functionality is really a pain because we don't have two events to know which event being fired.

But there are always some requirements :)

What I'm doing is setting one cookie in on-load and making a flag true if found that cookie and removing the cookie on browser close event.

So until unless the he closed the active tab(logged in tab), that cookie still there and if he tries to open in another tab, then the already active dialog comes.

Note:Solution provided with help of Cookies.

In Here is the onModule() for GWT / same as onload/document.ready() for java script/Jquery.

    @Override
        public void onModuleLoad() {
  if("already_in_browser".
          equalsIgnoreCase(Cookies.getCookie("already_in_browser"))){
                showAlreadyTabActiveDialog();
                return;
            }else{
                setLoggedincookie();
            }

 private void setLoggedincookie() {
         isLoggedintab = true;  //this is a global variable
         registerWindowCloseEvent();
         com.google.gwt.user.client.Cookies.
            setCookie("already_in_browser","already_in_browser");

    }

    private void showAlreadyTabActiveDialog() {
            alert("You are already active in another tab");
            registerWindowCloseEvent();
    }


    /**  This event is onbeforeunload in javascript

    private void registerWindowCloseEvent() {
         Window.addCloseHandler(new CloseHandler<Window>() {
                @Override
                public void onClose(CloseEvent<Window> event) {
                    if(isLoggedintab ){
                        Cookies.removeCookie("already_in_browser");
                    }
                }
            });

    }

Let me know If you found any bugs or loop holes in this.So that I'l look in to them.

I would be very happy,If some one provide a solution,without using cookies.

这篇关于区分浏览器选项卡关闭和刷新(以检测应用程序的多个实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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