onbeforeunload自定义对话框消息无法正常工作

时间:2016-08-01 12:46:06

标签: javascript html5 browser

我有一个场景,我需要显示一个对话框,要求用户重新检查他们所做的更改

因此,我正在进行onbeforeunload并希望收到一条消息“请重新检查您提供的评级和反馈”

$window.onbeforeunload = function() {
        var dialogText = 'Please recheck the rating and feedback that you have provided';
        return dialogText;
    };

但最终显示“您所做的更改可能无法保存”(默认消息)。

我不明白为什么我会这样做

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

你试过了吗?

$scope.$on('$destroy', function(e){
  // Show message
});

答案 1 :(得分:0)

请参阅MSDN doc https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload

  

基于WebKit的浏览器不遵循对话框的规范。

你应该尝试类似的东西:

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "Please recheck the rating and feedback that you have provided";
  e.returnValue = confirmationMessage;     // Gecko, Trident, Chrome 34+
  return confirmationMessage;              // Gecko, WebKit, Chrome <34
});