当前位置: 代码迷 >> 综合 >> 页面非正常关闭点击时间
  详细解决方案

页面非正常关闭点击时间

热度:34   发布时间:2024-01-29 09:52:53.0

第一次使用的案例,测试时他只是刷新页面,并没有使用户退出

window.onbeforeunload = function () {

         这有一个问题 window.event.screenX是一个空值,所以只刷新
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth - 20;

        if (b && window.event.clientY < 0 || window.event.altKey) {

            alert("是关闭而非刷新");

            window.open(this.location);

        } else {

            alert("是刷新而非关闭");

        } 
}

改为一下,事件成功

window.onbeforeunload = function() {
var n = window.event.screenX - window.screenLeft; 
var b = n > document.documentElement.scrollWidth - 20;
if (!(b && window.event.clientY < 0 || window.event.altKey)) {

           //你要执行的事件
$.post(contextpath + "/rest/user/adminlogout", function(res) {
}, "json");
}
}

  相关解决方案