setTimeout在IE11上无法正常工作

时间:2017-06-29 14:15:17

标签: javascript internet-explorer asynchronous

我有以下网页:

<html>
<head>
</head>
<body>
<h1>Wait until closed</h1>
<script>

function wait(popup){
      if (!popup.closed){
        setTimeout(wait,1000,popup);
      } else {
        alert('closed');
      } 
    }

var popup = window.open("http://www.google.com", '', 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes, width=1000 , height=800, top=' + screen.top + ', left=' + screen.left);

wait(popup); 

</script>
</body>
</html>

当我使用Chrome或Firefox打开它时,我会在关闭弹出窗口后看到“已关闭”消息。然而在IE11中没有任何反应。这种行为差异的解释是什么? (即IE11标准的哪一部分不遵守,或者在这种情况下有不同的解释,如果有的话)?

编辑:阅读建议的答案,我尝试将setTimeout(wait,1000,popup)更改为setTimeout(function() {wait(popup);},1000),如下所示:

<html>
<head>
</head>
<body>
<h1>Wait until closed</h1>
<script>

function wait(popup){
      if (!popup.closed){
        setTimeout( function() {
            wait(popup);
        }, 1000 );
      } else {
        alert('closed');
      } 
    }

var popup = window.open("http://www.google.com", '', 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes, width=1000 , height=800, top=' + screen.top + ', left=' + screen.left);

wait(popup); 

</script>
</body>
</html>

但这也不起作用。

编辑:评论表明这是重复的,但是因为根据建议的答案尝试更改代码直到现在才对我有用,我通过要求更改问题来修改问题上面的代码,以便它在IE11中工作(希望这是SO规则允许的)。显示的代码不起作用。

1 个答案:

答案 0 :(得分:1)

这个有点烦我,所以我在本地文件上试了一下并得到了同样的行为。 window.open在IE和Edge中都返回null。显然,如果启用了保护模式,window.open将在IE和Edge中返回null。

https://msdn.microsoft.com/en-us/library/Bb250462.aspx

我不确定如何解决它。也许是一个可以关闭的框架?