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