Onunload与Onbeforeunload

http://blog.sina.com.cn/s/blog_9e8a967c0100ynv4.html

Onunload与Onbeforeunload
Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来指定或者在<body>里指定。区别在于onbeforeunload在onunload之前执行,它还可以阻止onunload的执行。

<wbr><wbr><wbr>Onbeforeunload也是在页面刷新或关闭时调用,Onbeforeunload是正要去服务器读取新的页面时调用,此时还没开始读取;而 onunload则已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调用。Onunload是无法阻止页面的更新和关闭的。而 Onbeforeunload 可以做到。曾经做一个考试系统,涉及到防止用户半途退出考试(有意或者无意),代码如下:<br> Java代码<br> &lt;script type="text/javascript"&gt; <wbr><wbr><br> &lt;!-- <wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>window.onbeforeunload = onbeforeunload_handler; <wbr><wbr><br><wbr><wbr><wbr>window.onunload = onunload_handler; <wbr><wbr><br><wbr><wbr><wbr>function onbeforeunload_handler(){ <wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>var warning="确认退出?"; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return warning; <wbr><wbr><br><wbr><wbr><wbr>} <wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>function onunload_handler(){ <wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>var warning="谢谢光临"; <wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>alert(warning); <wbr><wbr><br><wbr><wbr><wbr>} <wbr><wbr><br> // --&gt; <wbr><wbr><br> &lt;/script&gt; <wbr><wbr><br><br> Java代码<br> 这段代码在FF和IE上都能正确执行.再点击关闭按钮时首先触发obbeforeunload事件,点击否定时不执行onload事件. <wbr><br><br> 这段代码在FF和IE上都能正确执行.再点击关闭按钮时首先触发obbeforeunload事件,点击否定时不执行onload事件.Java代码<br> 通常应用在 注销session等等登陆信息 等方面.... <wbr><br><br> 通常应用在 注销session等等登陆信息 等方面....Java代码<br> 这里一并推荐一个ActionScript3的好教程: &lt;A href="http://gskinner.com/talks/as3workshop/"&gt;http://gskinner.com/talks/as3workshop/&lt;/A&gt; <wbr><br><br> 这里一并推荐一个ActionScript3的好教程: http://gskinner.com/talks/as3workshop/写道<br> 运用onunload事件判断浏览器是刷新还是关闭窗口<br><wbr><br> 写道<br> function CloseOpen(event) {<br> if(event.clientX&lt;=0 &amp;&amp; event.clientY&lt;0) {<br> alert("关闭");<br> }<br> else<br> {<br> alert("刷新或离开");<br> }<br> }<br><br><wbr>Java代码<br> window.onbeforeunload = function() //author: meizz <wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>{ <wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>var n = window.event.screenX - window.screenLeft; <wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>var b = n &gt; document.documentElement.scrollWidth-20; <wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>if(b &amp;&amp; window.event.clientY &lt; 0 || window.event.altKey) <wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{ <wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>alert("是关闭而非刷新"); <wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>window.event.returnValue = ""; //这里可以放置你想做的操作代码 <wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

-------------------------------------------------------------------------------------------------------------------------------------

值得一提的是:我在使用onunload时(IE9)不起作用,换成onbeforeunload就可以,原因不详。

你可能感兴趣的:(onbeforeunload)