随笔 - 67,  文章 - 1,  评论 - 3,  阅读 - 89875

javascript捕获窗口关闭事件有两种方法 onbeforeunload()      ,onUnload()

用法有两种:

1.    function      window.onbeforeunload()      {      alert("关闭窗口")}

           function      window.onunload()      {      alert("关闭窗口")}

2.      在body 标签里加入onUnload事件

         <body onUnload="myClose()">

         然后在javascript里定义myClose()方法

 

 

区别:

          onUnload方法是在关闭窗口之后执行

          onbeforeUnload方法是在关闭窗口之前执行

 

说明:

          两个方法在 页面关闭、刷新、转向新页面 时都触发。

注:只在关闭窗口时触发,而页面刷新的时不触发。

<script       language=javascript>   
      function       window.onbeforeunload()   
      {   
      if       (event.clientX>document.body.clientWidth       &&       event.clientY<0||event.altKey)

            {   
             window.event.returnValue="确定要退出本页吗?";   
            }   

      }
</script>   

posted on   jixu  阅读(1300)  评论(0编辑  收藏  举报
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示