第一次使用的案例,测试时他只是刷新页面,并没有使用户退出
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");
}
}