使用Javascript从父窗口访问子窗口元素

5

我需要从父窗口访问子窗口元素。下面是我编写的示例代码片段。

父窗口 HTML:

<html>
<head>
<title>Parent</title>
<style>
div{
float:left;
cursor:pointer;
}
</style>
<script type="text/javascript">
var SubpopUpWin="";
function Opennew(passedURL){
    SubpopUpWin = window.open("popups.html", '_blank','toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes');  SubpopUpWin.document.getElementById("ifrm").src=passedURL;  
SubpopUpWin.document.getElementById("ifrm_title").innerHTML=passedURL;  

}
</script>
</head>
<body>
<div onclick="Opennew('http://www.google.com')">Google</div> 
<div onclick="Opennew('http://www.yahoo.com')">Yahoo</div>
<div onclick="Opennew('http://www.bing.com')">Bing</div>
</body>
</html>

popups.html

<html>
<head>
<title>Child</title>
<style>
div{
float:left;
}
</style>
</head>
<body>
<div>
  <div id="ifrm_title"></div>
  <div style="margin-top:20px">
   <iframe id="ifrm"  src="" width="470" height="270" frameborder="0" style="margin-top: 34px" scrolling="no"></iframe>
  </div>
</div>
</body>
</html>

在上述代码中出现了问题。即使我使用了下面的脚本也不起作用。
<script type="text/javascript">
var SubpopUpWin="";
function Opennew(passedURL){
    SubpopUpWin = window.open("popups.html", '_blank','toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes');
  SubpopUpWin.onload=function(){
     SubpopUpWin.document.getElementById("ifrm").src=passedURL;     
     SubpopUpWin.document.getElementById("ifrm_title").innerHTML=passedURL;     
  }
}
</script>

上述代码也无法正常工作。请分享您的建议/解决方案...
谢谢

passedURL 作为查询字符串传递给 popups.html 怎么样? - scibuff
我没有检查过那个...但是我们可以使用window.opener轻松地从子窗口访问父窗口元素。 - Madhan
好的,新窗口的DOM不会立即可用;尝试使用setTimeout。 - scibuff
是的,我知道。但是setTimeout也不起作用... - Madhan
通过使用查询字符串它可以工作... - Madhan
我也尝试过那种方法...最终我使用查询字符串完成了。 - Madhan
1个回答

7

我觉得这是出于安全方面的考虑。你可以尝试以下方式:

<html>
<head>
    <title>Parent</title>
    <style>
        div {
            float: left;
            cursor: pointer;
        }
    </style>
    <script type="text/javascript">
        var SubpopUpWin = "";
        var testUrl = "";
        function Opennew(passedURL){
            testUrl = passedURL;
            SubpopUpWin = window.open("popups.htm", '_blank', 'toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes');


        }
    </script>
</head>
<body>
    <div onclick="Opennew('http://www.google.com')">
        Google
    </div>
    <div onclick="Opennew('http://www.yahoo.com')">
        Yahoo
    </div>
    <div onclick="Opennew('http://www.bing.com')">
        Bing
    </div>
</body>

<html>
<head>
    <title>Child</title>
    <style>
        div {
            float: left;
        }
    </style>



</head>
<body>
    <div>
        <div id="ifrm_title">
        </div>
        <div style="margin-top:20px">
            <iframe id="ifrm" src="" width="470" height="270" frameborder="0" style="margin-top: 34px" scrolling="no">
            </iframe>
            <script type="text/javascript">

        document.getElementById("ifrm").src = window.opener.testUrl;

    </script>
        </div>
    </div>
</body>


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