Javascript 跨浏览器onbeforeunload?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7389554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Crossbrowser onbeforeunload?
提问by powtac
Does window.onbeforeunload()
fire in all browsers? I need a onbeforeunload functionality which is supported at least by IE6 and FF3.6.
是否window.onbeforeunload()
在所有浏览器中触发?我需要至少 IE6 和 FF3.6 支持的 onbeforeunload 功能。
For IE, onbeforeunload()
seems only to be supported by IE9
对于 IE,onbeforeunload()
似乎只有IE9 支持
回答by Aelios
I found a workaround for Firefox with setTimeout
function because it does not have the same behaviour as other web browsers.
我找到了一种具有setTimeout
功能的Firefox 解决方法,因为它与其他 Web 浏览器的行为不同。
window.onbeforeunload = function (e) {
var message = "Are you sure ?";
var firefox = /Firefox[\/\s](\d+)/.test(navigator.userAgent);
if (firefox) {
//Add custom dialog
//Firefox does not accept window.showModalDialog(), window.alert(), window.confirm(), and window.prompt() furthermore
var dialog = document.createElement("div");
document.body.appendChild(dialog);
dialog.id = "dialog";
dialog.style.visibility = "hidden";
dialog.innerHTML = message;
var left = document.body.clientWidth / 2 - dialog.clientWidth / 2;
dialog.style.left = left + "px";
dialog.style.visibility = "visible";
var shadow = document.createElement("div");
document.body.appendChild(shadow);
shadow.id = "shadow";
//tip with setTimeout
setTimeout(function () {
document.body.removeChild(document.getElementById("dialog"));
document.body.removeChild(document.getElementById("shadow"));
}, 0);
}
return message;
}
GitHub: https://github.com/Aelios/crossbrowser-onbeforeunload
GitHub: https://github.com/Aelios/crossbrowser-onbeforeunload
回答by Ian Stanway
No it does not fire in all browsers. It's not supported in mobile browsers e.g. Safari, Opera Mobile & mini, Dolphin. See Is there an alternative method to use onbeforeunload in mobile safari?
不,它不会在所有浏览器中触发。它在移动浏览器中不受支持,例如 Safari、Opera Mobile & mini、Dolphin。请参阅是否有其他方法可以在移动 Safari 中使用 onbeforeunload?
回答by Jacek Pietal
Building upon Tushar Ahirrao solution this works cross browser and triggers once (Works in Firefox, Chrome, whatever)
建立在 Tushar Ahirrao 解决方案的基础上,它可以跨浏览器工作并触发一次(适用于 Firefox、Chrome 等)
<html>
<head>
<script type="text/javascript">
var app = {};
app.unloaded = false;
app.unload = function() {
if (app.unloaded) return; else app.unloaded = true;
// your code here
return "YO";
};
</script>
</head>
<body onunload="return app.unload();" onbeforeunload="return app.unload();">
YO
</body>
</html>
Paste above template to empty file then edit it
将上面的模板粘贴到空文件然后编辑它
回答by Brad Christie
It's my recollection that IE was the only browser to implement onbeforeunload
, but some browsers have taken it upon themselves to implement it.
我记得 IE 是唯一实现 IE 的浏览器onbeforeunload
,但有些浏览器已经自行实现了它。
Long story short, IE is about the only browser (with very finite exceptions) you'll find this event consistently in.
长话短说,IE 是唯一一个你会发现这个事件始终存在的浏览器(有非常有限的例外)。
回答by Tushar Ahirrao
Then you can use both like this :
然后你可以像这样使用两者:
<body onunload="functionName();" onbeforeunload='functionName();' >