我们希望在用户关闭浏览器之前抛出特定的消息。是否有一种机制适用于所有主流浏览器(如IE,firefox,chrome,opera)
答案 0 :(得分:5)
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';
}
};
答案 1 :(得分:0)
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome etc.
});