70

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

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

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

火狐4确认

可以覆盖此默认消息吗?

4

4 回答 4

52

来自MDN

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

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

于 2011-03-22T22:46:21.290 回答
32

除了上面的答案,我还改进了解决方法。

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

$(window).bind('beforeunload', function() {
    if(/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 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?";
    }
});

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

于 2012-03-26T04:08:38.050 回答
3

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

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

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

于 2012-03-15T10:20:05.203 回答
1

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

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

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

于 2011-05-13T15:04:35.947 回答