浏览器关闭时如何注销用户

浏览器关闭时如何注销用户

问题描述:

在这里,我想在用户关闭浏览器时注销.为此,我完成了研发工作,发现在关闭浏览器时将触发以下代码.

Here I want to logout an user when they close the browser. For that I have done R&D and found that the following code will fire when we close the browser.

window.onbeforeunload = function() {
  myService.logout();
  return 'Your own message goes here...';
}

在这里,当我尝试关闭浏览器时,将触发此事件,并使用户注销.但是这里的问题是,当页面被重定向时,也会触发该事件.

Here when I try to close the browser this event will fire and it will make the user to logout. But here the problem is when the page is redirected that time also this event is firing.

我想使用此功能使用户注销.但这是错误的.请帮助我完成此功能.

I want to use this function to make the user to logout.But it is going wrong.Please help me to do this functionality.

但是这里的问题是,当页面被重定向时,也会触发此事件.

But here the problem is when the page is redirected that time also this event is firing.

我想这是您自己正在执行的重定向.如果是这种情况,为什么不使用全局变量将重定向与客户端的重定向区分开?像这样:

I guess this is a redirect that you yourself are performing. If that's the case, why don't you use a global variable to differ your redirects with your client's redirects? Something like this:

...
thisismyredirect = true; //before redirecting, set this variable
window.location = "http://www.yoururl.com";
...

,并在您的 onbeforeunload 事件中,检查此重定向是否由您执行.如果是,则无需调用 logout()函数:

and in your onbeforeunload event, you check whether this redirect was performed by you or not. If yes, then no need to call logout() function:

window.onbeforeunload = function() {
    if(!thisismyredirect) {
        myService.logout();
        return 'Your own message goes here...';
    }
}