DIV CSS 佈局教程網

用 JavaScript 判斷網頁是關閉還是刷新
編輯:關於JavaScript     

原理是透過離開頁面的 unload 行為觸發時機,去偵測當下的瀏覽器視窗大小,再依此判斷使用者是刷新、跳轉還是關閉的行為。

程式碼如下:

 window.onunload = function(){ var a_n = window.event.screenX - window.screenLeft; var a_b = a_n > document.documentElement.scrollWidth-20; if(a_b && window.event.clientY< 0 || window.event.altKey){ alert('關閉頁面行為'); }else{ alert('跳轉或者刷新頁面行為'); } }

用瀏覽器右上角的關閉按鈕時還蠻好用,但在分頁上關閉、或從工作列關閉時,這個方法就不管用了。

其實 JS 只有 onload、onunload、onbeforeunload 事件,並沒有 onclose。而且不論是關閉還是刷新,都會執行 unload。那該如何捕捉真正的關閉呢?關鍵在於各事件的執行順序:

頁面載入時只執行 onload;
頁面關閉時只執行 unload;
頁面刷新時則先執行 onbeforeunload,然後 unload,最後 onload。

因此我們可以在 onbeforeunload 中打一個標記,再在 unload 中判斷該標記,就能分辨頁面是否真的被關閉。以下是一個更完整、相容 Firefox 的版本:

 <mce:script type="text/javascript"><!-- function close(evt) //author: sunlei { var isIE=document.all?true:false; evt = evt ? evt :(window.event ? window.event : null); if(isIE){//IE浏覽器 var n = evt.screenX - window.screenLeft; var b = n > document.documentElement.scrollWidth-20; if(b && evt.clientY<0 || evt.altKey){ //alert("是關閉而非刷新"); window.location.href="../include/logout.php"; } else{ //alert("是刷新而非關閉"); return false; } } else{//火狐浏覽器 if(document.documentElement.scrollWidth!=0) { //alert("是刷新而非關閉"); //window.location.href="report_list.php?ss=1"; return false; } else{ alert("是關閉而非刷新"); //window.location.href="repost_list.php?ss=0"; //alert("bbbbbbb"); } } } // --></mce:script> <BODY onunload="close(event);"> 

上面的方法仍無法判斷多標籤頁的瀏覽器(如 360、IE8 這類),接著再看:

 function CloseOpen(event) { if(event.clientX<=0 || event.clientY<0) { //獲取當前時間 var date=new Date(); //將date設置為過去的時間 alert("關閉網頁"); date.setTime(date.getTime()-10000); //將userId這個cookie刪除 document.cookie="zhuangtao;expire="+date.toUTCString(); document.cookie="quanxianzifucuan;expire="+date.toUTCString(); document.cookie="quanxian;expire="+date.toUTCString(); s0 += "關閉窗口!"; sw = 1; onbeforeunload(); // window.event.returnValue = '關閉浏覽器將退出系統.'; } else { alert("刷新或離開"); } } var currentKeyCode = -1; function document.onkeydown() { // 本窗口的所有下屬頁面都必須含有本函數 top.currentKeyCode = event.keyCode; } function onbeforeunload(){ var sw = 0, s0 = ""; if (currentKeyCode == 116) { s0 += "刷新窗口!(F5)"; } else { if ((event.altKey)&&(currentKeyCode == 115)) { s0 += "關閉窗口!(alt+F4)"; sw = 1; //獲取當前時間 var date=new Date(); //將date設置為過去的時間 alert("關閉窗口"); date.setTime(date.getTime()-10000); //將userId這個cookie刪除 document.cookie="zhuangtao;expire="+date.toUTCString(); document.cookie="quanxianzifucuan;expire="+date.toUTCString(); document.cookie="quanxian;expire="+date.toUTCString(); } else { if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth)) { s0 += "刷新窗口!"; } else { //獲取當前時間 var date=new Date(); //將date設置為過去的時間 alert("關閉網頁"); date.setTime(date.getTime()-10000); //將userId這個cookie刪除 document.cookie="zhuangtao;expire="+date.toUTCString(); document.cookie="quanxianzifucuan;expire="+date.toUTCString(); document.cookie="quanxian;expire="+date.toUTCString(); s0 += "關閉窗口!"; sw = 1; } } } if (sw == 1) { event.returnValue = ""; } else { currentKeyCode = -1; } } <body onunload="CloseOpen(event)" ></body></html>

這版除了工作列關閉外基本都能應付,大致能滿足我們的需求。以上就是本文關於用 JavaScript 判斷網頁是關閉還是刷新的介紹,希望大家喜歡。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved