简体   繁体   English

为什么浏览器刷新和关闭事件检测不适用于IE和Safari

[英]Why browser refresh and close event detection is not working for IE and Safari

I need to call logout function on the close of browser window gets close. 我需要在浏览器窗口关闭时调用logout function来关闭。 It's working fine in Chrome not working in IE and Safari. Chrome在IE和Safari中无法正常运行。

I have tried the following code: 我尝试了以下代码:

window.onbeforeunload = function (e) {
    // Your logic to prepare for 'Stay on this Page' goes here
    var evtobj = window.event ? event : e;
    if (evtobj == e) {
        //firefox
        if (!evtobj.clientY) {
           //server call
        }
    }
    else {
        //IE
        if (evtobj.clientY < 0) {
           //server call
        }
    }
    //return "Please click 'Stay on this Page' and we will give you candy";
};

I have tried a few other ways but they didn't work. 我尝试了其他几种方法,但是它们没有用。 Please advise. 请指教。

There is something wrong in your design, you SHOULDN'T rely on a client-side hook to perform logout. 您的设计有问题,您不应依赖客户端挂钩执行注销。 There are one billion of reasons why that event could not be executed. 有十亿个原因导致该事件无法执行。 Just limit the onbeforeunload event to execute informational content and not critical actions. 只需限制onbeforeunload事件即可执行信息内容,而不是执行关键操作。

By the way: 顺便说说:

  • Don't return in your beforeunload event! 不要返回您的beforeunload事件! This creates some issue in IE 这在IE中造成一些问题
  • Use window.sessionStorage to make some data last until the user closes the tab 使用window.sessionStorage使一些数据持续到用户关闭选项卡
  • Use session cookies to store your user's sensitive data like as tokens, and check if a user is logged on the server, and not on the client 使用会话cookie来存储用户的敏感数据(如令牌),并检查用户是否登录在服务器上,而不是在客户端上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM