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、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>



在点击浏览器关闭按钮关闭浏览器的时候,有时不希望它直接关闭,希望弹出对话框提示客户,这个时候就需要用到事件onbeforeunload,用法如下:
<script language="javascript">
<!--
function opCheckUnload(){
  if(g_blnCheckUnload){
    try{
      window.event.returnValue = "if close the window,the auto task will complete with document no update version";
    }catch(e){}
  }
}
-->
</script>

 

<body onbeforeunload="opCheckUnload();">
</body>

不过这种用法不支持firefox,因为firefox不支持window.event,所以本人改了一种方式,应用如下:
window.onbeforeunload = function() {
   if(g_blnCheckUnload){
     return("if close the window,the auto task will complete with document no update version!");
   }
}



使用onbeforeunload属性后的副作用

    在使用博客园的编辑器编辑文章时,有一个挺体贴用户的功能,就是如果用户进入编辑器后,不是执行"POST"操作而要退出当前编辑器页面,我们会得到一个Alert窗口提示(如下图)。这个功能其实很简单,就是通过处理window、body或frameset对象或元素的onbeforeunload事件实现的。

    PromptDialog.png

    这个unbeforeunload的具体用法我就不说了,反正几乎能引起当前页面发生跳转的任何动作,都会触发这个事件。并且它先于unload事件被触发,如果在其的事件处理函数里面向event.returnValue属性赋值非空字符串,就会出现一个如上图的窗口(代码如下)。

window.onbeforeunload = function ()
{
    
// . . .  
    event.returnValue = "You will lose any unsaved content";
    
// . . .    
}

    至于这个窗口是做什么用的,窗口上系统提示的文字(第1行和第3行)已经说的非常清楚了。那么使用这个事件到底有什么问题呢?

    我们知道引起当前页面发生条转主要3类事件:
    1、对浏览器窗口的操作,比如关闭浏览器、go home、backward、forward和refresh等;
    2、对于网页的Navigatie操作,比如点击链接、在地址栏中执行新的地址、页面表单提交到非当前页面等;
    3、脚本代码引起的页面Navigate操作,比如执行navigate、locatoin方法(replace, reload, assign)和修改location属性(href, search)等。

    以上的1-2类操作引起onbeforeunload的事件,使用起来基本没有任何的问题,可是执行第3类页面条转操作却有点小问题。就是在上图的窗口出现后,如果点选"OK",确实就OK,页面正确跳转了。可是如果这个时候,我们点选"Cancel",却会出现一个脚本异常,如下图:

    ErrorDialog.png

    解决方法就是把脚本调用try一下,但这似乎是IE的一个bug的说。

try
{
    
//   
    event.returnValue = "You will lose any unsaved content";
    
//   
}

catch(e){}
posted on 2010-05-18 22:47  PentiumZ  阅读(2159)  评论(0编辑  收藏  举报