首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

[请教您确定要离开本页吗?]的实现方法

2012-10-06 
[请问您确定要离开本页吗?]的实现方法??? onunload、onbeforeunload都是在刷新或关闭时调用,可以在script

[请问您确定要离开本页吗?]的实现方法

??? 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+
  示例:
????? a)
??? ? <html xmlns=" http://www.w3.org/1999/xhtml">
   ??? <head>
   ??? ? <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   ??? ? <title>onbeforeunload测试</title>
   ??? <script>
   ??? ??? function checkLeave(){
????      event.returnValue="确定离开当前页面吗?";
   ??? ??? }
   ??? ? </script>
   ? </head>
   ? <body onbeforeunload="checkLeave()"></body>
   </html>
??? ? b)
??? ? <html xmlns=" http://www.w3.org/1999/xhtml">
   ??? <head>
   ??? ? <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   ??? ? <title>onbeforeunload测试</title>
   ??? <script>
   ??? ??? function checkLeave(){
????      event.returnValue="确定离开当前页面吗?";
   ??? ??? }
??? ??? ??? window.onbeforeunload = checkLeave;
   ??? ? </script>
   ? </head>
   ? <body></body>
   </html>

??? 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的表单的时候。
  示例:
????? a)
????? <html xmlns="http://www.w3.org/1999/xhtml">
   ? <head>
   ??? <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   ??? <title>onunload测试</title>
   ??? <script>
   ????? function checkLeave(){
????    ?? alert("欢迎下次再来!");
   ????? }
   ??? </script>
   ? </head>
   ? <body onunload="checkLeave()"></body>
   </html>
??? ? b)
????? <html xmlns="http://www.w3.org/1999/xhtml">
   ? <head>
   ??? <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   ??? <title>onunload测试</title>
   ??? <script>
   ????? function checkLeave(){
????    ?? alert("欢迎下次再来!");
   ????? }
??? ??? ??? window.onunload=checkLeave;
   ??? </script>
   ? </head>
   ? <body></body>
   </html>

??? ---------------------------------------我是华丽的分割线------------------------------------------
??? onbeforeunload与a标签在IE中的冲突bug

??? onbeforeunload是window的一个事件,目前Firefox,IE都支持,主要用来提示用户是否真的要离开该页面,通常在一些比较重要的数据提交之前,防止用户误操作导致数据丢失。典型的应用如gmail中,在写邮件的时候,如果刷新页面或者关闭页面,会出现提示。
??? 但是在IE下点击一些a标签时,也会触发onbeforeunload事件。并且href中写javascript:void(0)也不行,而在Firefox中不会出现类似的情况。于是查资料对onbeforeunload事件重新认识了一下:
??? a标签触发事件的顺序
??? onclick、onbeforeunload跟href三者之间的先后运行关系是这样的:onclick > onbeforeunload > href,知道了这个道理,我们就可以通过一些方法阻止onbeforeunload。另外在IE浏览器中,假如href为#,那么也不会触发 onbeforeunload事件。
??? 怎么阻止onbeforeunload
??? 在Ajax的同时,给a标签加上onclick事件,这样onclick在onbeforeunload之前运行,然后来个return false,就可以啦~
??? 绕过onbeforeunload直接href
??? 结合onclick事件,我们可以绕过onbeforeunload直接href,下面的代码就可以绕过onbeforeunload而执行href:
??? var a=1;
??? window.onbeforeunload=function()
??? {
??? ??? if(a)alert("onbeforeunload事件爆发了!");
??? }
??? 只要我们在onclick事件加上一个a=0;就可以了~
??? 实例
??? 此处使用了window.onbeforeunload对onclick、onbeforeunload和href进行的测试。

热点排行