简体   繁体   English

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

[英]Access child window elements from parent window using Javascript

I need to access child window elements from parent window. 我需要从父窗口访问子窗口元素。 I have written the sample snippets below. 我已经在下面写了示例片段。

Parent HTML: 父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 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>

In the above code is not working. 在上面的代码不起作用。 Even I have used the below script also. 即使我也使用了下面的脚本。

<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>

The above code also not working. 上面的代码也行不通。 Please share your sugestions/solutions... 请分享您的sugestions / solutions ...

Thanks 谢谢

I feel this is because of some security. 我觉得这是因为一些安全问题。 You can try this way instead: 您可以尝试这种方式:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM