Javascript Firefox 4 onBeforeUnload 自定义消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5398772/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:01:33  来源:igfitidea点击:

Firefox 4 onBeforeUnload custom message

javascriptfirefoxfirefox4

提问by JoJo

In Firefox 3, I was able to write a custom confirmation popup with:

在 Firefox 3 中,我能够编写一个自定义确认弹出窗口:

window.onbeforeunload = function() {
   if (someCondition) {
      return 'Your stream will be turned off';
   }
}

Now in Firefox 4, it does not show my custom message. The default message that it provides is not even accurate to what my application does.

现在在 Firefox 4 中,它不会显示我的自定义消息。它提供的默认消息甚至与我的应用程序所做的不准确。

firefox 4 confirm

火狐4确认

Can this default message be overridden?

可以覆盖此默认消息吗?

采纳答案by ThiefMaster

From MDN:

来自MDN

Note that in Firefox 4 and later the returned string is not displayed to the user. See Bug 588292.

请注意,在 Firefox 4 及更高版本中,返回的字符串不会显示给用户。请参阅错误588292

This "Bug" is actually a (imho questionable) feature.. so there's no way to display the message in Firefox 4. If you think it should be changed, comment on that bug so the Firefox developers will know that people actually want to be able to show a custom string.

这个“错误”实际上是一个(恕我直言有问题的)功能..所以没有办法在 Firefox 4 中显示该消息。如果您认为应该更改它,请对该错误发表评论,以便 Firefox 开发人员知道人们实际上想要成为能够显示自定义字符串。

回答by Nasif

Addition to the above Answer, I have improved the workaround.

除了上述答案之外,我还改进了解决方法。

I have used jquery here. you can use default javascript funciton as well.

我在这里使用了 jquery。您也可以使用默认的 javascript 函数。

$(window).bind('beforeunload', function() {
    if(/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.) >= 4) {
        if(confirm("Are you Sure do you want to leave?")) {
            history.go();
        } else {
            window.setTimeout(function() {
                window.stop();
            }, 1);
        }
    } else {
        return "Are you Sure do you want to leave?";
    }
});

Tested and working in firefox 11 as well. :)

也在 Firefox 11 中进行了测试和工作。:)

回答by xmedeko

My workaround is to show alert in onbeforeunload:

我的解决方法是在 onbeforeunload 中显示警报:

window.onbeforeunload=function() {
    if ( /Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.) >= 4) {
        alert("Blah blah. You have to confirm you are leaving this page in the next dialogue.");
    }
    return "Blah blah."; 
} 

(It shows two dialogues in Firefox, one dialogue elsewhere.)

(它在 Firefox 中显示了两个对话,在其他地方显示了一个对话。)

回答by Jguru

Try implementing it with a confirm message,

尝试使用确认消息实现它,

window.onbeforeunload=function(){
   return confirm("Are you sure??");
}

of course when the user confirms then the FF4 message is shown, so you maybe better display this once per site on login/visit. A cookie should do the trick.

当然,当用户确认时会显示 FF4 消息,因此您最好在登录/访问时为每个站点显示一次。饼干应该可以解决问题。