js如何区分刷新事件和关闭事件
弄了一个用户关闭浏览器非法退出系统,
使用onbeforeunload和onunload 操作,在网上看了看基本上都是使用
window.onbeforeunload=function (){
alert("===onbeforeunload===");
if(event.clientX>document.body.clientWidth && event.clientY < 0 || event.altKey){
alert("你关闭了浏览器");
}else{
alert("你正在刷新页面");
}
}
window.onbeforeunload=function (){
alert("===onbeforeunload===");
if(event.clientX>document.body.clientWidth && event.clientY < 0
[解决办法]
event.altKey){
alert("你关闭了浏览器");
}else{
alert("你正在刷新页面");
}
return false;
}
1.这中写法IE中支持较好
2.Chrome 仅会弹出是否离开此页/是否重新加载此页。不会执行alert。
3.FireFox 会执行alert后关闭窗口。
没有过多研究,请见谅
我在IE9下测试也是不行嘚
window.onbeforeunload=function (){
alert("===onbeforeunload===");
if(event.clientX>document.body.clientWidth && event.clientY < 0
[解决办法]
event.altKey){
alert("你关闭了浏览器");
}else{
alert("你正在刷新页面");
}
return false;
}
加 return false; 了吗?