无法获取 window.open() 的 innerHTML

Cannot get the innerHTML of a window.open()

HI 我在用window.open()打开的window里面的文字有些问题,希望大家能帮帮我,我需要的是我的parentwindow 将读取 parent window 调用的 child window 的 <div> html 值, 我有这段代码,但它不起作用

Parent Window

<button onclick="buttonset()">Click me</button>
<script>
var myWindow ;
 function buttonset () {
         myWindow = window.open("pag1.html", "myWindow", "width=200,height=100");
    //myWindow.document.write("<div id='hola'>This is 'myWindow'</div>");
    alert(myWindow.document.getElementById("result").innerHTML);
    myWindow.close();
}
</script>

这是我的 Child Window (pag1.html) 代码

    <body>

<div id="result">1</div>
<div id="msj">Correcto</div>

</body>

当我 运行 它说只是打开新的 window 但它没有显示消息

Cannot read property 'innerHTML' of null

使用 load 打开的事件 window

myWindow.onload = function() {
  // do stuff
}

window.open()是异步的,需要等待文档加载。

function buttonset () {
    var myWindow = window.open("pag1.html", "myWindow", "width=200,height=100");
    myWindow.onload = function() {
        alert(myWindow.document.getElementById("result").innerHTML);
        myWindow.close();
    };
}

页面仍在加载中。所以你可以在 pag1.html

的 body 标签上附加一个内联 onload 函数

pag1.html

<body onload='doIt()'>   
  <div id="result">1</div>
  <div id="msj">Correcto</div>
  <script>
     function doIt() {
       alert(document.getElementById("result").innerHTML);
    }
   </script>
</body>