卸载前跨浏览器打开?

发布于 2024-12-04 07:22:05 字数 342 浏览 1 评论 0原文

window.onbeforeunload() 是否在所有浏览器中触发?我需要一个至少 IE6 和 FF3.6 支持的 onbeforeunload 功能。

对于 IE,onbeforeunload() 似乎只是 IE9 支持

Does window.onbeforeunload() fire in all browsers? I need a onbeforeunload functionality which is supported at least by IE6 and FF3.6.

For IE, onbeforeunload() seems only to be supported by IE9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苯莒 2024-12-11 07:22:05

我通过 setTimeout 函数找到了 Firefox 的解决方法,因为它的行为与其他网络浏览器不同。

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

I found a workaround for Firefox with setTimeout function because it does not have the same behaviour as other web browsers.

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

痴梦一场 2024-12-11 07:22:05

不,它不会在所有浏览器中触发。移动浏览器不支持它,例如 Safari、Opera Mobile 和 Opera Mobile。迷你,海豚。请参阅 是否有其他方法可以使用 onbeforeunload移动游猎?

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?

2024-12-11 07:22:05

建立在 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>

将上面的模板粘贴到空文件,然后对其进行编辑

Building upon Tushar Ahirrao solution this works cross browser and triggers once (Works in Firefox, Chrome, whatever)

<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

甜心小果奶 2024-12-11 07:22:05

我的回忆是,IE是唯一实现OnBeForeunLoad的浏览器,但是一些浏览器已将其自行实施。

长话短说,IE是唯一的浏览器(有限的例外),您会始终发现此事件。

It's my recollection that IE was the only browser to implement onbeforeunload, but some browsers have taken it upon themselves to implement it.

Long story short, IE is about the only browser (with very finite exceptions) you'll find this event consistently in.

深海不蓝 2024-12-11 07:22:05

然后你可以像这样使用两者:

<body onunload="functionName();" onbeforeunload='functionName();' >

Then you can use both like this :

<body onunload="functionName();" onbeforeunload='functionName();' >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文