JavaScript監(jiān)聽瀏覽器刷新或是關(guān)閉事件Word版_第1頁
JavaScript監(jiān)聽瀏覽器刷新或是關(guān)閉事件Word版_第2頁
JavaScript監(jiān)聽瀏覽器刷新或是關(guān)閉事件Word版_第3頁
JavaScript監(jiān)聽瀏覽器刷新或是關(guān)閉事件Word版_第4頁
JavaScript監(jiān)聽瀏覽器刷新或是關(guān)閉事件Word版_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、JS監(jiān)聽關(guān)閉瀏覽器事件關(guān)鍵字: js監(jiān)聽關(guān)閉瀏覽器事件 Onunload與Onbeforeunload Onunload,onbeforeunload都是在刷新或關(guān)閉時調(diào)用,可以在<script>腳本中通過window.onunload來指定或者在<body>里指定。區(qū)別在于onbeforeunload在onunload之前執(zhí)行,它還可以阻止onunload的執(zhí)行。 Onbeforeunload也是在頁面刷新或關(guān)閉時調(diào)用,Onbeforeunload是正要去服務(wù)器讀取新的頁面時調(diào)用,此時還沒開始讀??;而onunload則已經(jīng)從服務(wù)器上讀到了需要加載的新的頁面,在即將替換

2、掉當(dāng)前頁面時調(diào)用。Onunload是無法阻止頁面的更新和關(guān)閉的。而 Onbeforeunload 可以做到。曾經(jīng)做一個考試系統(tǒng),涉及到防止用戶半途退出考試(有意或者無意),代碼如下: Java代碼 <script type="text/javascript"> <!- window.onbeforeunload = onbeforeunload_handler; window.onunload = onunload_handler; function onbeforeunload_handler() var warning="確認(rèn)退出?"

3、; return warning; function onunload_handler() var warning="謝謝光臨" alert(warning); / -> </script> <script type="text/javascript"> <!- window.onbeforeunload = onbeforeunload_handler; window.onunload = onunload_handler; function onbeforeunload_handler() var warning=

4、"確認(rèn)退出?" return warning; function onunload_handler() var warning="謝謝光臨" alert(warning); / -> </script> Java代碼 這段代碼在FF和IE上都能正確執(zhí)行.再點擊關(guān)閉按鈕時首先觸發(fā)obbeforeunload事件,點擊否定時不執(zhí)行onload事件. 這段代碼在FF和IE上都能正確執(zhí)行.再點擊關(guān)閉按鈕時首先觸發(fā)obbeforeunload事件,點擊否定時不執(zhí)行onload事件.Java代碼 通常應(yīng)用在 注銷session等等登陸信息 等方面.

5、 通常應(yīng)用在 注銷session等等登陸信息 等方面.Java代碼 這里一并推薦一個ActionScript3的好教程: <A href=" 這里一并推薦一個ActionScript3的好教程: 運用onunload事件判斷瀏覽器是刷新還是關(guān)閉窗口 寫道 function CloseOpen(event) if(event.clientX<=0 && event.clientY<0) alert("關(guān)閉"); else alert("刷新或離開"); Java代碼 window.onbeforeunload =

6、 function() /author: meizz var n = window.event.screenX - window.screenLeft; var b = n > document.documentElement.scrollWidth-20; if(b && window.event.clientY < 0 | window.event.altKey) alert("是關(guān)閉而非刷新"); window.event.returnValue = "" /這里可以放置你想做的操作代碼 <script langu

7、age=javascript>function window.onbeforeunload() if(event.clientX>document.body.clientWidth&&event.clientY<0|event.altKey) window.event.returnvalue = "" </script>網(wǎng)站制作,找田響沒錯!或者使用全屏打開頁面網(wǎng)站制作,找田響沒錯!<script language="javascript"><!-window.open(,"32p

8、ic","fullscreen=3,height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");-></script>網(wǎng)站制作,找田響沒錯!注:在body標(biāo)簽里加上onbeforeunload="javascript:return false"(使不能關(guān)閉窗口)本文來自: 田響建站() 詳細(xì)出處參考:網(wǎng)頁制作,網(wǎng)站制作,找田響沒錯!=function open

9、url() /需要打開的地址 koyoz.launchURL(''); function openinit() document.body.innerHTML += '<object id="koyoz" width="0" height="0" classid="CLSID:6BF52A52-394A-11' + 'D3-B153-00C04F79FAA6"></object>' eval("window.attachEvent(&

10、#39;onload',openinit);"); eval("window.attachEvent('onunload',openurl);");=function getEvent() /同時兼容ie和ff的寫法 if(document.all) return window.event; func=getEvent.caller; while(func!=null) var arg0=func.arguments0; if(arg0) if(arg0.constructor=Event | arg0.constructor =Mouse

11、Event) | (typeof(arg0)="object" && arg0.preventDefault && arg0.stopPropagation) return arg0; func=func.caller; return null; window.onbeforeunload = function() var n = window.event.screenX - window.screenLeft; var b = n > document.documentElement.scrollWidth-20; if(b &

12、;& window.event.clientY < 0 | window.event.altKey) if(confirm("是否有參與網(wǎng)上調(diào)查?") koyoz.launchURL(''); =本來寫這篇博客,不是為了解決這個問題的,我的初衷是做一個網(wǎng)頁瀏覽統(tǒng)計的,本來以為用標(biāo)題描述的方法可以實現(xiàn),其實我是走了一個誤區(qū)。不必用JS我也可以達(dá)到我的目的,但是為了實現(xiàn)標(biāo)題描述的問題,我還是從網(wǎng)上找了很多資料,但是發(fā)現(xiàn)一個問題:在 IE下好用,可是到了火狐下就不好用了。于是乎,我做了一些測試,發(fā)現(xiàn)以下方法可以在IE和火狐下通用:<script

13、 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.client

14、Y<0 | evt.altKey) alert("是關(guān)閉而非刷新"); else alert("是刷新而非關(guān)閉"); else/火狐瀏覽器 if(document.documentElement.scrollWidth!=0) alert("是刷新而非關(guān)閉"); else alert("是關(guān)閉而非刷新"); </script><body onunload="close(event);"> 其中參數(shù)event是一定要傳進(jìn)去的,因為在火狐下如果不傳的話,它會報錯:win

15、dow.event is not defined。當(dāng)然,在IE下如果不傳的話,是沒有問題的。 不過細(xì)心的人會發(fā)現(xiàn),其實在火狐下進(jìn)行判斷的時候根本沒有用到evt。其實把evt傳進(jìn)去,只是為了保證瀏覽器不會報錯,其實可以做如下修改,效果是一樣的:<script type="text/javascript=script language=javascript window.onbeforeunload = function() /author: meizz var n = window.event.screenX - window.screenLeft; var b = n docu

16、ment.documentElement.scrollWidth-20; if(b window.event.clientY 0 | window.event.altKey) aler<script language="javascript"> window.onbeforeunload = function() /author: meizz var n = window.event.screenX - window.screenLeft; var b = n > document.documentElement.scrollWidth-20; if(b

17、&& window.event.clientY < 0 | window.event.altKey) alert("是關(guān)閉而非刷新"); window.event.returnValue = "" /這里可以放置你想做的操作代碼 else alert("是刷新而非關(guān)閉"); </script><SCRIPT>function window.onbeforeunload() if(event.clientX>document.body.clientWidth&&even

18、t.clientY<0|event.altKey) window.event.returnValue="如果離開該頁面,將有可能無法獲得誠信標(biāo)簽" else alert("你在刷新") ; </SCRIPT> function window.onbeforeunload() if(event.clientX>document.body.clientWidth&&event.clientY<0|event.altKey) window.event.returnValue="如果離開該頁面,將有可能無法

19、獲得誠信標(biāo)簽" else alert("你在刷新") ; </SCRIPT><HTML><HEAD><TITLE>判斷是刷新還是關(guān)閉www.51windows.Net</TITLE><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><META NAME="Author" CONTENT="51windows,海娃,haiwa"

20、;><META NAME="Description" CONTENT="Power by 51windows.Net"></HEAD><script>function CloseOpen(event) if(event.clientX<=0 && event.clientY<0) alert("關(guān)閉");elsealert("刷新或離開");</script><body onunload="CloseOpen(even

21、t)"></BODY></HTML><div style="position: absolute; top: 10; right: 10; width: 148; height: 18;cursor:hand"><input type="button" name="Button" value="查看源代碼" onClick= 'window.location = "view-source:" + window.location.h

22、ref'></div> -<script language=javascript> function window.onbeforeunload() if (event.clientX>document.body.clientWidth && event.clientY<0|event.altKey) window.event.returnValue="確定要退出本頁嗎?" </script>-<script language=javascript>function window.onbeforeunload()if(event.clientX>document.body.clientWidth&&event.clientY<0|event.altKey)var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");xmlh

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論