在點擊瀏覽器關(guān)閉按鈕關(guān)閉瀏覽器的時候,有時不希望它直接關(guān)閉,希望彈出對話框提示客戶,這個時候就需要用到事件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 onbeforeunload="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!");
?? }
}
這樣幾種瀏覽器都支持了,彈出對話框如下:
但這里有個問題,本來還沒有解決的,我用的瀏覽器是中文版,但我做的系統(tǒng)是英文版,這個事件彈出來的窗口所顯示的語言隨瀏覽器,沒有辦法自己在javascript里設(shè)置。