目标window.opener iframe

时间:2013-09-03 13:50:06

标签: javascript html iframe

我可以像这样写一些iframe

window.frames[0].document.write(something);

但我正在尝试从同一域中打开的窗口向iframe写一些东西。我不知道如何定位那个iframe。我试过这样的事情:

window.opener.document.getElementById("iframe").write(something);

window.opener.frames[0].document.write(something);

但那没用。关于我想做什么的任何想法或问题?

* iframe位于父窗口中。我尝试从打开的窗口中定位

1 个答案:

答案 0 :(得分:1)

您需要使用iframe的contentWindow属性。试试这个。

父页

<iframe id="ifr"></iframe>

<button onclick="window.open('child.html')">Open Window</button>

Child.html页面

<button onclick="writeToParentIframe()">Write to Parent Iframe</button>

<script>
    function writeToParentIframe() {
        opener.document.getElementById("ifr").contentWindow.document.write("<h1>Hello World</h1>")
    }
</script>
相关问题