带有动态内容和空白url的JavaScript新窗口,未调用事件侦听器

带有动态内容和空白url的JavaScript新窗口,未调用事件侦听器,javascript,Javascript,案例1: var printWindow = window.open("", "print_window"); if(printWindow) { printWindow.addEventListener('load', function(){alert(1);}, false); //Everything works but this listener is never called printWindow.document.write(printView); pr

案例1:

var printWindow = window.open("", "print_window");
if(printWindow) {

    printWindow.addEventListener('load', function(){alert(1);}, false); //Everything works but this listener is never called

    printWindow.document.write(printView);
    printWindow.focus();
}
案例2:

var printWindow = window.open("<html>useless random stuff</html>", "print_window");
if(printWindow) {

    printWindow.addEventListener('load', function(){alert(1);}, false); //this is hit
    printWindow.focus();
}
那么,将HTML动态加载到新窗口文档会取消加载事件侦听器吗?还是我真的把事情搞砸了?
需要帮助:D

您遇到一些文档上下文问题。敬仰和期待

还要检查下面的代码段,并注意如何从不同的窗口名称空间调用警报函数

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
<button id="window_opener">Click me to load a new window</button>
<script type="text/javascript">
  document.getElementById("window_opener").addEventListener('click', function(){
    var badDocumentString = "<html><head><title>print window</title></head><body>I live in child window</body></html>";
    var child = window.open()
    childdoc = child.document
    childdoc.write(badDocumentString);
    childdoc.close();
    child.addEventListener("load", function(){
      child.alert("zeee germans"); //attention here
    });
  })
</script>
</body>
</html>

但是,在我当前的设置中,你的代码片段不起作用。当我删除childDoc.write并直接将html作为参数传递给window open时,它就起作用了。使用chrome/ium,是的,我刚刚意识到这不起作用。试试firefox。或者将window.load=alertyour_消息注入badDocumentString变量,使代码也能在chrome上运行。我已经解决了我的主要问题,因此我认为我不需要继续此childWindow加载侦听器方式…无论如何,谢谢: