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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM