2

I can write something to an iframe like this

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

But i am trying to write something to an iframe from a opened window within the same domain. I don't know how to target that iframe. I tried something like this:

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

or

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

But that didn't work. Any ideas or questions about what i am trying to do?

*The iframe is on the parent window. I try to target that from the opened window

Koiski
  • 568
  • 2
  • 8
  • 23
  • So to get it clear. You are opening a new window with an `iframe`. And from the parent window (which opened the new window) you want to write something in that new opened window's `iframe`? – putvande Sep 03 '13 at 13:59
  • No, I am opening a new window. From that window i try to write something to the iframe on the parent window – Koiski Sep 03 '13 at 14:01
  • http://stackoverflow.com/a/6581896/1427878 – CBroe Sep 03 '13 at 14:14

1 Answers1

1

You need to use contentWindow property of the iframe. Try this.

Parent page

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

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

Child.html page

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

<script>
    function writeToParentIframe() {
        opener.document.getElementById("ifr").contentWindow.document.write("<h1>Hello World</h1>")
    }
</script>
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136