onbeforeunload与onunload事件(转)
Onunload,onbeforeunload都是在刷新或關(guān)閉時(shí)調(diào)用,可以在<script> 腳本中通過window.onunload來指定或者在<body>里指定。區(qū)別在于onbeforeunload在onunload之前執(zhí)行,它還可以阻止onunload的執(zhí)行。Onbeforeunload也是在頁面刷新或關(guān)閉時(shí)調(diào)用,Onbeforeunload是正要去服務(wù)器讀取新的頁面時(shí)調(diào)用,此時(shí)還沒開始讀取;而onunload則已經(jīng)從服務(wù)器上讀到了需要加載的新的頁面,在即將替換掉當(dāng)前頁面時(shí)調(diào)用。Onunload是無法阻止頁面的更新和關(guān)閉的。而 Onbeforeunload 可以做到。1、onbeforeunload事件:
說明:目前三大主流瀏覽器中firefox和IE都支持onbeforeunload事件,opera尚未支持。
用法:
·object.onbeforeunload = handler
·<element onbeforeunload = “handler” … ></element>
描述:
事件觸發(fā)的時(shí)候彈出一個(gè)有確定和取消的對(duì)話框,確定則離開頁面,取消則繼續(xù)待在本頁。handler可以設(shè)一個(gè)返回值作為該對(duì)話框的顯示文本。
觸發(fā)于:
·關(guān)閉瀏覽器窗口
·通過地址欄或收藏夾前往其他頁面的時(shí)候
·點(diǎn)擊返回,前進(jìn),刷新,主頁其中一個(gè)的時(shí)候
·點(diǎn)擊 一個(gè)前往其他頁面的url連接的時(shí)候
·調(diào)用以下任意一個(gè)事件的時(shí)候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.
·當(dāng)用window open打開一個(gè)頁面,并把本頁的window的名字傳給要打開的頁面的時(shí)候。
·重新賦予location.href的值的時(shí)候。
·通過input type=”submit”按鈕提交一個(gè)具有指定action的表單的時(shí)候。
可以用在以下元素:
·BODY, FRAMESET, window
平臺(tái)支持:
IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, Firefox0.9+
示例:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>onbeforeunload測(cè)試</title>
<script>
function checkLeave(){
????event.returnValue="確定離開當(dāng)前頁面嗎?";
}
</script>
</head>
<body οnbefοreunlοad="checkLeave()">
</body>
</html>
?
2、onunload事件
用法:
·object.onbeforeunload = handler
·<element onbeforeunload = "handler"></element>
描述:
當(dāng)用戶關(guān)閉一個(gè)頁面時(shí)觸發(fā) onunload 事件。
觸發(fā)于:
·關(guān)閉瀏覽器窗口
·通過地址欄或收藏夾前往其他頁面的時(shí)候
·點(diǎn)擊返回,前進(jìn),刷新,主頁其中一個(gè)的時(shí)候
·點(diǎn)擊 一個(gè)前往其他頁面的url連接的時(shí)候
·調(diào)用以下任意一個(gè)事件的時(shí)候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.
·當(dāng)用window open打開一個(gè)頁面,并把本頁的window的名字傳給要打開的頁面的時(shí)候。
·重新賦予location.href的值的時(shí)候。
·通過input type=”submit”按鈕提交一個(gè)具有指定action的表單的時(shí)候。
示例:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>onunload測(cè)試</title>
<script>
function checkLeave(){
????alert("歡迎下次再來!");
}
</script>
</head>
<body οnunlοad="checkLeave()">
</body>
</html>
轉(zhuǎn)載于:https://www.cnblogs.com/shoupifeng/archive/2012/04/23/2466017.html
總結(jié)
以上是生活随笔為你收集整理的onbeforeunload与onunload事件(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JQuery中ajax方法访问web服务
- 下一篇: nothing about web ap