用js判斷頁面刷新或關(guān)閉的方法(onbeforeunload與onunload事件)
更新時(shí)間:2012年06月22日 13:42:13 作者:
Onunload,onbeforeunload都是在刷新或關(guān)閉時(shí)調(diào)用,可以在<script>腳本中通過window.onunload來指定或者在<body>里指定
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í)還沒開始讀?。欢鴒nunload則已經(jīng)從服務(wù)器上讀到了需要加載的新的頁面,在即將替換掉當(dāng)前頁面時(shí)調(diào)用。Onunload是無法阻止頁面的更新和關(guān)閉的。而 Onbeforeunload 可以做到。
頁面加載時(shí)只執(zhí)行onload
頁面關(guān)閉時(shí)先執(zhí)行onbeforeunload,最后onunload
頁面刷新時(shí)先執(zhí)行onbeforeunload,然后onunload,最后onload。
1、onbeforeunload事件:
說明:目前三大主流瀏覽器中firefox和IE都支持onbeforeunload事件,opera尚未支持。
用法:
·object.onbeforeunload = handler
·<element onbeforeunload = “handler” … ></element>
描述:
事件觸發(fā)的時(shí)候彈出一個(gè)有確定和取消的對話框,確定則離開頁面,取消則繼續(xù)待在本頁。handler可以設(shè)一個(gè)返回值作為該對話框的顯示文本。
觸發(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測試</title>
<script>
function checkLeave(){
event.returnValue="確定離開當(dāng)前頁面嗎?";
}
</script>
</head>
<body onbeforeunload="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測試</title>
<script>
function checkLeave(){
alert("歡迎下次再來!");
}
</script>
</head>
<body onunload="checkLeave()">
</body>
</html>
一個(gè)判斷頁面是否真的關(guān)閉和刷新的好方法:
window.onbeforeunload=function (){
alert("===onbeforeunload===");
if(event.clientX>document.body.clientWidth && event.clientY < 0 || event.altKey){
alert("你關(guān)閉了瀏覽器");
}else{
alert("你正在刷新頁面");
}
}
這段代碼就是判斷觸發(fā)onbeforeunload事件時(shí),鼠標(biāo)是否點(diǎn)擊了關(guān)閉按鈕,或者按了ALT+F4來關(guān)閉網(wǎng)頁,如果是,則認(rèn)為系統(tǒng)是關(guān)閉網(wǎng)頁,否則在認(rèn)為系統(tǒng)是刷新網(wǎng)頁。
Onbeforeunload也是在頁面刷新或關(guān)閉時(shí)調(diào)用,Onbeforeunload是正要去服務(wù)器讀取新的頁面時(shí)調(diào)用,此時(shí)還沒開始讀?。欢鴒nunload則已經(jīng)從服務(wù)器上讀到了需要加載的新的頁面,在即將替換掉當(dāng)前頁面時(shí)調(diào)用。Onunload是無法阻止頁面的更新和關(guān)閉的。而 Onbeforeunload 可以做到。
頁面加載時(shí)只執(zhí)行onload
頁面關(guān)閉時(shí)先執(zhí)行onbeforeunload,最后onunload
頁面刷新時(shí)先執(zhí)行onbeforeunload,然后onunload,最后onload。
1、onbeforeunload事件:
說明:目前三大主流瀏覽器中firefox和IE都支持onbeforeunload事件,opera尚未支持。
用法:
·object.onbeforeunload = handler
·<element onbeforeunload = “handler” … ></element>
描述:
事件觸發(fā)的時(shí)候彈出一個(gè)有確定和取消的對話框,確定則離開頁面,取消則繼續(xù)待在本頁。handler可以設(shè)一個(gè)返回值作為該對話框的顯示文本。
觸發(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+
示例:
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>onbeforeunload測試</title>
<script>
function checkLeave(){
event.returnValue="確定離開當(dāng)前頁面嗎?";
}
</script>
</head>
<body onbeforeunload="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í)候。
示例:
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>onunload測試</title>
<script>
function checkLeave(){
alert("歡迎下次再來!");
}
</script>
</head>
<body onunload="checkLeave()">
</body>
</html>
一個(gè)判斷頁面是否真的關(guān)閉和刷新的好方法:
復(fù)制代碼 代碼如下:
window.onbeforeunload=function (){
alert("===onbeforeunload===");
if(event.clientX>document.body.clientWidth && event.clientY < 0 || event.altKey){
alert("你關(guān)閉了瀏覽器");
}else{
alert("你正在刷新頁面");
}
}
這段代碼就是判斷觸發(fā)onbeforeunload事件時(shí),鼠標(biāo)是否點(diǎn)擊了關(guān)閉按鈕,或者按了ALT+F4來關(guān)閉網(wǎng)頁,如果是,則認(rèn)為系統(tǒng)是關(guān)閉網(wǎng)頁,否則在認(rèn)為系統(tǒng)是刷新網(wǎng)頁。
相關(guān)文章
使用JavaScript獲取URL中的參數(shù)(兩種方法)
這篇文章主要介紹了使用JavaScript獲取URL中的參數(shù)(兩種方法)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11JS實(shí)現(xiàn)回到頁面頂部動(dòng)畫效果的簡單實(shí)例
下面小編就為大家?guī)硪黄狫S實(shí)現(xiàn)回到頁面頂部動(dòng)畫效果的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05AJAX跨域請求json數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章介紹了AJAX跨域請求json數(shù)據(jù)的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-11-11js 數(shù)組操作之pop,push,unshift,splice,shift
本篇文章主要介紹了js數(shù)組操作之pop,push,unshift,splice,shift。需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01認(rèn)識(shí)Knockout及如何使用Knockout綁定上下文
Knockout簡稱ko,是一個(gè)輕量級(jí)的javascript類庫,采用MVVM設(shè)計(jì)模式(即Model、view、viewModel),簡單優(yōu)雅的實(shí)現(xiàn)了雙向綁定,實(shí)時(shí)更新,幫助您使用干凈的數(shù)據(jù)模型來創(chuàng)建豐富的、響應(yīng)式的用戶界面2015-12-12JavaScript中兩種鏈?zhǔn)秸{(diào)用實(shí)現(xiàn)代碼
方法鏈一般適合對一個(gè)對象進(jìn)行連續(xù)操作(集中在一句代碼)。一定程度上可以減少代碼量,缺點(diǎn)是它占用了函數(shù)的返回值。2011-01-01