使用Javascript从父窗口访问子窗口元素 [英] Access child window elements from parent window using Javascript

查看:39
本文介绍了使用Javascript从父窗口访问子窗口元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从父窗口访问子窗口元素.我已经在下面编写了示例代码片段.

I need to access child window elements from parent window. I have written the sample snippets below.

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

在上面的代码中不起作用.甚至我也用过以下脚本.

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

谢谢

推荐答案

我认为这是出于安全考虑.您可以改用这种方式:

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>

这篇关于使用Javascript从父窗口访问子窗口元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆