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
.