在点击浏览器关闭按钮关闭浏览器的时候,有时不希望它直接关闭,希望弹出对话框提示客户,这个时候就需要用到事件onbeforeunload,用法如下:
<script language="javascript">
<!--
function opCheckUnload(){
if(g_blnCheckUnload){
try{
window.event.returnValue = "if close the window,the auto task will complete with document no update version";
}catch(e){}
}
}
-->
</script>
<body οnbefοreunlοad="opCheckUnload();">
</body>
不过这种用法不支持firefox,因为firefox不支持window.event,所以本人改了一种方式,应用如下:
window.onbeforeunload = function() {
if(g_blnCheckUnload){
return("if close the window,the auto task will complete with document no update version!");
}
}
<script language="javascript">
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 = ""; //这里可以放置你想做的操作代码
}
}
</script>
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 = ""; //这里可以放置你想做的操作代码
}
}
</script>