惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hacker News: Ask HN
Hacker News: Ask HN
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
The Last Watchdog
The Last Watchdog
TaoSecurity Blog
TaoSecurity Blog
Schneier on Security
Schneier on Security
SecWiki News
SecWiki News
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
O
OpenAI News
W
WeLiveSecurity
Security Archives - TechRepublic
Security Archives - TechRepublic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
H
Hacker News: Front Page
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
P
Privacy & Cybersecurity Law Blog
K
Kaspersky official blog
S
Security @ Cisco Blogs
Latest news
Latest news
AWS News Blog
AWS News Blog
U
Unit 42
Martin Fowler
Martin Fowler
阮一峰的网络日志
阮一峰的网络日志
S
Secure Thoughts
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Know Your Adversary
Know Your Adversary
Scott Helme
Scott Helme
博客园 - 司徒正美
B
Blog RSS Feed
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Docker
Google Online Security Blog
Google Online Security Blog
Jina AI
Jina AI
aimingoo的专栏
aimingoo的专栏
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Last Week in AI
Last Week in AI
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
SegmentFault 最新的问题
NISL@THU
NISL@THU
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
小众软件
小众软件

博客园 - 痞子刘

20款实用经典的Chrome插件推荐,不过要自己到扩展中心搜噢! jQuery扩展loadthumb,用jQuery实现图片预加载和等比例缩小 新书推荐《锋利的jQuery》 jQuery 1.3.3 新功能 Twitter新手教程 jQuery性能优化指南(3) jQuery性能优化指南(2) jQuery性能优化指南(1) 50个最好的wordpress插件 26款你可能从未听过的浏览器 养成使用jQuery Sizzle选择器的良好习惯 有关css和js针对不同浏览器兼容的问题 K-MeleonCCF浏览器 jquery 1.3正式版发布了 你是最适合创业的程序员吗? 1st JavaScript Editor Pro 3.8,强大的JavaScript开发工具(JavaScript IDE),完美支持CSS, HTML, DOM and DHTML开发、校验、调试 file上传控件onchange事件失效的解决方法 Content-Disposition的使用和注意事项 ASP.NET生成JSON字符串
onbeforeunload与onunload事件
痞子刘 · 2009-06-10 · via 博客园 - 痞子刘

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>

但是onbeforeunload有个小毛病,就是页面刷新时,他还是会调用到onbeforeunload,为什么?其实刷新就相当于关闭了这个IE再重新打开的意思,因此还是会调用到onbeforeunload。

 究竟怎么解决刷新不调用onbeforeunload呢?

 网上提供很多方法,本人觉得最实用还是以下这段JS

window.onbeforeunload   =   function(){   
      
var   n   =   window.event.screenX   -   window.screenLeft;   
      
var   b   =   n   >   document.documentElement.scrollWidth-20;   
      
if(b   &&   window.event.clientY   <   0   ||   window.event.altKey)   
      {   
          alert(
"是关闭而非刷新");   
          window.event.returnValue   
=   "是否关闭?";
      }
else{
             alert(
"是刷新而非关闭");   
     }   
}

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>