js 监听浏览器刷新还是关闭事件

监听页面关闭:

window.onbeforeunload = function() {
    //鼠标相对于用户屏幕的水平位置 - 窗口左上角相对于屏幕左上角的水平位置 = 鼠标在当前窗口上的水平位置
    var n = window.event.screenX - window.screenLeft;
    //鼠标在当前窗口内时,n<m,b为false;鼠标在当前窗口外时,n>m,b为true。20这个值是指关闭按钮的宽度
    var b = n > document.documentElement.scrollWidth-20;
    //鼠标在客户区内时,window.event.clientY>0;鼠标在客户区外时,window.event.clientY<0
    if(b && window.event.clientY < 0 || window.event.altKey || window.event.ctrlKey){
              //关闭浏览器时你想做的事
        alert("关闭");
    }else if(event.clientY > document.body.clientHeight || event.altKey){
              //刷新浏览器时你想做的事
        alert("刷新");
    }
}

亲测可用2018年1月19日14:48:01 !

转自:  http://udn.yyuap.com/forum.php?mod=viewthread&tid=96309

原文地址:https://www.cnblogs.com/jooy/p/8316806.html