简体   繁体   English

使用javascript访问iframe的父项

[英]Acces to the parent of the iframe with javascript

There is a website ( S ), that contains an iframe ( A ), that displays my site. 有一个网站( S ),其中包含显示我的网站的iframe( A )。 What I need, is to add an iframe ( B ) from my site to the parent page ( S ) with javascript. 我需要的是使用JavaScript将我的网站中的iframe( B )添加到父页面( S )。 I coded something, but this adds the new iframe ( B ) to the iframe's ( A ) content, not to the parent's ( S ) 我编写了一些代码,但这会将新的iframe( B )添加到iframe( A )的内容中,而不是添加到父级( S )的内容中

The website ( S ) is not the same domain. 网站( S )不是同一域。

Code on my site: 我网站上的代码:

<script>

    function GetFrame() 
    { 
        ifrm = window.document.createElement("IFRAME"); 
        ifrm.setAttribute("src", "http://stackoverflow.com/"); 
        ifrm.setAttribute("style", "position: absolute; left: 0px; top: 0px; width: 100%; height: 25%; border: none;"); 

        window.document.getElementsByTagName('BODY')[0].appendChild(ifrm);
    } 

</script>


<img src="image.jpg" onload="GetFrame()"/>

You can't do this because of the Same Origin Policy . 由于同源政策,您无法执行此操作。

If you want to add iframe B to S , you'll need S to run the script for you, either by fetching it with <script src="SCRIPT_URL"></script> or by defining it inline with <script>YOUR_SCRIPT</script> . 如果要将iframe B添加到S中 ,则需要S为您运行脚本,方法是使用<script src="SCRIPT_URL"></script>获取它,或者通过<script>YOUR_SCRIPT</script>

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

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