关闭窗口时触发事件(刷新时不触发onunload)

Posted on 2006-11-16 星期四

  要在关闭窗口时触发一个事件,我们会想到window对象提供了两个事件:onUnload和onBerforUnload,因为它们是针对document的,当document被卸载或被卸载前触发。但这两个事件在刷新时也会触发,所以我们要采用什么方法来屏蔽除了关闭窗口操作以外而触发的onUnload和onBerforUnload事件从而执行里面的操作呢?代码如下:]

  1. <script>
  2. window.onunload = function(){if(self.screenTop>9000)alert(‘该窗口已经被关闭!’)}
  3. </script>
  4.  
  5.  
  6. <script>
  7. window.onunload = function(){if(self.screenLeft>9000)alert(该窗口已经被关闭!.‘)}
  8. </script>

原理如下:
window.screenTop
获取浏览器客户区左上角相对于屏幕左上角的 y 坐标
screenTop>后面的数字必须大于你显示分辩率中的高度
比如,800*600, 这个数得大于600
window.screenLeft
获取浏览器客户区左上角相对于屏幕左上角的 x 坐标
screenLeft>后面的数字必须大于你显示分辩率中的宽度
比如,800*600, 这个数得大于800

通常这两个值设为9000


No comments have been added to this post yet.

留下评论

(必需)

(必需)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post | TrackBack URI