on('load') 不适用于 Internet Explorer 11 中打开的弹出窗口

on('load') does not work for opened popup in internet explorer 11

我不记得上次使用弹出窗口是什么时候了,但是:(

我想打开新的 window,当它打开时我想大喊“1”。 Window 在所有浏览器中打开,但在 IE 11 / Edge 中它不会 alert()。

<html>
<head>
<script src="//code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(document).ready(function(){
  $('#button').on('click',function(e){
    e.preventDefault();
    var win = window.open ('/test.html')
    $(win).on('load',function(){
      alert ('opened');
    });
  });
});
</script>
</head>
<body>
<a href="#" id="button">button</a>
</body>
</html>

去掉 window.open 之后和括号之前的 space ,这样你就不会启动 JS 引擎。您可能会得到 win 持有 window.open 函数的副本,而不是新的 window 本身。

虽然在定义函数参数列表的括号前放置 space 可能是一种风格选择,如:

// Either of these will work
function foo (a, b, c) { ...  
function foo(a, b, c) { ...

在方法名称之后和分组该方法参数的括号之前,永远不应有 space:

obj.foo(x);  // Correct
obj.foo (x); // Incorrect

注意:由于沙盒,此代码不会在此处的 Stack Overflow 代码片段区域执行。

$(document).ready(function(){
  
  // Set the "button" to have a click callback
  $('#button').on('click',function(e){

      // Cancel the native click event of the hyperlink
      e.preventDefault();

      // Open a new window and get a handle to it
      var win = window.open('/test.html');  // <-- No space after "open"

      // Set the new window to have a load event handler
      $(win).on("load", function(e){
        
        // Alert the user
        alert("Yay!");
      });

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="#" id="button">button</a>

发生这种情况是因为 IE11(可能还有 Edge)在 window 打开完成加载之前不会从 window 打开调用返回。您添加的加载事件处理程序未被调用,因为该事件已经触发。

file: 协议文件不断请求允许被阻止的内容和崩溃的 IE 后,测试使用本地主机节点恢复。js/express 服务器。

通过在 test.html 中放置一个加载事件侦听器来验证结果,其中包括将 "test loaded<br>" 附加到 DIV 元素的内容的代码,并将代码放置在点击处理程序中将 "window opened<br>" 附加到相同的 DIV,在打开的页面中,紧接在 window 打开调用之后。

IE 按此顺序报告 "test loaded, window opened"。 Firefox 在打开页面中抛出错误,用于登录打开页面的 DIV 元素尚不存在(正如预期的那样)。

如果不知道 inter-window 访问需要实现什么以及您控制和可以修改哪些文件,则很难提出编码解决方案。知道问题出在哪里应该会有所帮助!