2010-04-25 64 views

回答

5

檢查onbeforeunload機制:

window.onbeforeunload = function (e) { 
    var e = e || window.event; 

    if (has_message_to_throw) { 

    // For IE and Firefox 
    if (e) { 
     e.returnValue = 'Specific message'; 
    } 

    // For Safari 
    return 'Specific message'; 

    } 


}; 
+0

我嘗試了以下功能,但它不適用於IE 8,最新版本的Opera。我在這裏錯過了什麼? window.onbeforeunload =功能(E){VAR 消息= 「你確認消息放在這裏。」 é= E || window.event; //對於IE和Firefox 如果(E){ e.returnValue =消息; } //對於野生 返回消息; }; – Sam 2010-04-26 11:17:54

+0

@Samuel:看起來Opera不支持'onbeforeunload'(參考:http://www.zachleat.com/web/2008/04/22/dont-let-the-door-hit-you-onunload-and -onbeforeunload /)。 – kennytm 2010-04-26 16:18:46

+1

IE8也沒有工作,這對我們來說非常重要。 – Sam 2010-04-27 10:41:37

0

Firefox Documentation

window.addEventListener("beforeunload", function (e) { 
    var confirmationMessage = "\o/"; 

    (e || window.event).returnValue = confirmationMessage;  //Gecko + IE 
    return confirmationMessage;        //Webkit, Safari, Chrome etc. 
}); 
相關問題