Differentiate browser tab close and refresh (to detect multiple instances of the application)

How can I differentiate the browser tab and update the functionality.

Currently, the refresh and close window event has no different events.

My requirement is to check that the weather user is already registered or not in any of the tabs, so I do not allow him to download my application to 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 activated both for updating and for closing events. Is there a way to differentiate.

+2
source share
1 answer

Differentiating the closing of the browser tab and refresh is really a pain, because we do not have two events to find out which event will be fired.

But there are always some requirements :)

What I do is set one cookie to on-load and make the flag true if it is found to be a cookie and delete the cookie in the browser close event.

So, until he closes the active tab (login), this cookie still exists, and if he tries to open on another tab, an already active dialog box will appear.

Note. A cookie provided solution Cookies.

In Here, onModule () for GWT / is the 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 find any errors or holes in the loop in it. Therefore, I will look at them.

I would be very happy if someone provides a solution without using cookies .

+3
source

All Articles