在點擊瀏覽器關閉按鈕關閉瀏覽器的時候,有時不希望它直接關閉,希望彈出對話框提示客戶,這個時候就需要用到事件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!");
}
}
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>