离开页面的判断:window.Onunload与window.onbeforeunload的区别
2013-12-09 17:21
567 查看
原地址:http://www.luwei.net.cn/view/ad1d36d9d89e90ac32d2ee06
Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或者在<body>里指定。
区别在于onbeforeunload在onunload之前执行,它还可以阻止onunload的执行。
Onbeforeunload也是在页面刷新或关闭时调用,Onbeforeunload是正要去服务器读取新的页面时调用,此时还没开始读取;而onunload则已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调用。Onunload是无法阻止页面的更新和关闭的。而 Onbeforeunload 可以做到。
1、onbeforeunload事件:
说明:目前三大主流浏览器中firefox和IE都支持onbeforeunload事件,opera尚未支持。
用法:
·object.onbeforeunload = handler
·<element onbeforeunload = “handler” … ></element>
描述:
事件触发的时候弹出一个有确定和取消的对话框,确定则离开页面,取消则继续待在本页。handler可以设一个返回值作为该对话框的显示文本。
触发于:
·关闭浏览器窗口
·通过地址栏或收藏夹前往其他页面的时候
·点击返回,前进,刷新,主页其中一个的时候
·点击 一个前往其他页面的url连接的时候
·调用以下任意一个事件的时候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.
·当用window open打开一个页面,并把本页的window的名字传给要打开的页面的时候。
·重新赋予location.href的值的时候。
·通过input type=”submit”按钮提交一个具有指定action的表单的时候。
可以用在以下元素:
·BODY, FRAMESET, window
平台支持:
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=”确定离开当前页面吗?”;
}
</script>
</head>
<body onbeforeunload=”checkLeave()”>
</body>
</html>
示例2:
window.onbeforeunload = function() {
if(已经输入不少内容了){
return "确认要离开此页面吗?";
}
};
2、onunload事件
用法:
·object.onbeforeunload = handler
·<element onbeforeunload = “handler”></element>
描述:
当用户关闭一个页面时触发 onunload 事件。
触发于:
·关闭浏览器窗口
·通过地址栏或收藏夹前往其他页面的时候
·点击返回,前进,刷新,主页其中一个的时候
·点击 一个前往其他页面的url连接的时候
·调用以下任意一个事件的时候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.
·当用window open打开一个页面,并把本页的window的名字传给要打开的页面的时候。
·重新赋予location.href的值的时候。
·通过input type=”submit”按钮提交一个具有指定action的表单的时候。
示例:
<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>
Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或者在<body>里指定。
区别在于onbeforeunload在onunload之前执行,它还可以阻止onunload的执行。
Onbeforeunload也是在页面刷新或关闭时调用,Onbeforeunload是正要去服务器读取新的页面时调用,此时还没开始读取;而onunload则已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调用。Onunload是无法阻止页面的更新和关闭的。而 Onbeforeunload 可以做到。
1、onbeforeunload事件:
说明:目前三大主流浏览器中firefox和IE都支持onbeforeunload事件,opera尚未支持。
用法:
·object.onbeforeunload = handler
·<element onbeforeunload = “handler” … ></element>
描述:
事件触发的时候弹出一个有确定和取消的对话框,确定则离开页面,取消则继续待在本页。handler可以设一个返回值作为该对话框的显示文本。
触发于:
·关闭浏览器窗口
·通过地址栏或收藏夹前往其他页面的时候
·点击返回,前进,刷新,主页其中一个的时候
·点击 一个前往其他页面的url连接的时候
·调用以下任意一个事件的时候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.
·当用window open打开一个页面,并把本页的window的名字传给要打开的页面的时候。
·重新赋予location.href的值的时候。
·通过input type=”submit”按钮提交一个具有指定action的表单的时候。
可以用在以下元素:
·BODY, FRAMESET, window
平台支持:
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=”确定离开当前页面吗?”;
}
</script>
</head>
<body onbeforeunload=”checkLeave()”>
</body>
</html>
示例2:
window.onbeforeunload = function() {
if(已经输入不少内容了){
return "确认要离开此页面吗?";
}
};
2、onunload事件
用法:
·object.onbeforeunload = handler
·<element onbeforeunload = “handler”></element>
描述:
当用户关闭一个页面时触发 onunload 事件。
触发于:
·关闭浏览器窗口
·通过地址栏或收藏夹前往其他页面的时候
·点击返回,前进,刷新,主页其中一个的时候
·点击 一个前往其他页面的url连接的时候
·调用以下任意一个事件的时候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.
·当用window open打开一个页面,并把本页的window的名字传给要打开的页面的时候。
·重新赋予location.href的值的时候。
·通过input type=”submit”按钮提交一个具有指定action的表单的时候。
示例:
<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>
相关文章推荐
- 离开页面的判断:window.Onunload与window.onbeforeunload的区别(IE下a标签触发问题)
- window.onunload与window.onbeforeunload的区别
- js离开页面执行函数 onbeforeunload与onunload事件
- 用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)
- 用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件) 详细出处参考:http://www.jb51.net/article/30640.htm
- 获取页面离开onbeforeunload与onunload事件的返回值
- 用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)
- js离开页面执行函数 onbeforeunload与onunload事件
- 获取页面离开onbeforeunload与onunload事件的返回值
- 用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)
- 用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)
- 关于判断离开当前页面(刷新或关闭)时触发函数onbeforeunload有时触发有时不触发问题
- js离开页面执行函数 onbeforeunload与onunload事件
- 【实例】javascript中的window对象的onbeforeunload、onload、onunload的区别(在新版本的浏览器中两个关闭事件可能不会触发)
- 用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)
- 用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)
- window.Onunload与window.onbeforeunload的区别
- onbeforeunload事件--------可以用来写(当用户尝试离开页面时脚本会警告用户将丢失数据)
- 页面离开事件 onbeforeunload
- Onload,Onunload,onbeforeunload,$(window).load(function() {})和$(document).ready(function(){})