为什么在使用window.onbeforeunload时IE11会抛出错误?

5
我把我的问题分解为以下简单的代码:

我已经分解了以下简单的代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>IE test</title>
    <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
    <script type="text/javascript" charset="utf-8">
      $( document ).ready(function() {
        $('.js-click').click(function(e){
          window.location.href = 'http://www.google.com/';
        });
        window.onbeforeunload = function(e){
          return 'moving on';
        };
      });
    </script> 
  </head>
<body>
  <a href="#" class="js-click">Google</a>
</body>
</html>

在 Chrome 中,这个功能可以正常运行,没有警告或错误。但是,在 IE11 中,当你选择“留在此页面”时,会出现以下错误:

File: 10.0.1.126:8080, Line: 10, Column: 11

有什么想法吗?

在我的IE 11.0.9600.17842中运行良好。 - Arun P Johny
我正在运行11.0.9600.17843。它并没有真正弹出错误对话框,但我在JavaScript控制台中看到了那个消息。你有没有看过那里? - Ricky Nelson
1个回答

6

实际上,错误来自于:

window.location.href = 'http://www.google.com/';

这只是一种猜测,但我认为IE的开发人员想要能够捕获用户决定不遵循链接时的状态。因此,您可以尝试使用try-catch来获取此块的信息,并且您将知道用户没有被重定向(由于onbeforeunload的结果)。

try {
    window.location.href = 'http://www.google.com';
} catch (error) {
    alert("Y U NO REDIRECT");
}

如果您使用console.log(error),你会发现没有错误信息,并且错误编号为-2147467259。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接