问题描述
我需要在浏览器窗口关闭时调用logout function
来关闭。
Chrome在IE和Safari中无法正常运行。
我尝试了以下代码:
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";
};
我尝试了其他几种方法,但是它们没有用。 请指教。
1楼
Cristian Traìna
1
2019-02-21 09:28:53
您的设计有问题,您不应依赖客户端挂钩执行注销。
有十亿个原因导致该事件无法执行。
只需限制onbeforeunload
事件即可执行信息内容,而不是执行关键操作。
顺便说说:
-
不要返回您的
beforeunload
事件! 这在IE中造成一些问题 -
使用
window.sessionStorage
使一些数据持续到用户关闭选项卡 - 使用会话cookie来存储用户的敏感数据(如令牌),并检查用户是否登录在服务器上,而不是在客户端上