3

As answered in this post, I'm trying to set an Iframe's content with :

document.getElementById("iframe").contentDocument.write("Test123");

The problem is that even though everything seems to work (the Iframe is displaying Test123), the browser doesn't stop loading. Indeed, in Firefox's tab it's written "Connecting ..." with the spinner indicating it's still loading.

Why does it happen ? Is there a better solution to set an Iframe's content ?

Community
  • 1
  • 1
Jecimi
  • 4,113
  • 7
  • 31
  • 40
  • you need to close the connection: ocument.getElementById("iframe").contentDocument.close(); (I think that this will work). –  Aug 27 '13 at 13:39
  • How exactly are you writing to contentDocument if it is supposed to be readonly? https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement – ars265 Aug 27 '13 at 13:40
  • Perhaps the attribute is read only but the Document that is returned is read/write. Ohh well we learn something new everyday. – ars265 Aug 27 '13 at 13:43
  • 1
    @jeff : Thanks ! It works :), could you post a full answer so I can validate it as the solution. – Jecimi Aug 27 '13 at 13:43
  • @ars256 : I don't know exactly why, but I can confirm it works. – Jecimi Aug 27 '13 at 13:48

2 Answers2

11

You need to close the iframe connection:

document.getElementById("iframe").contentDocument.close();

I think that this will work.

M.Svrcek
  • 5,485
  • 4
  • 25
  • 33
0

Try doing it with jQuery, it might solve your issue and it is much cleaner:

$("#iframe").contents().find("html").html("Test123")
CodePuppet
  • 191
  • 4