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