Filling an IFRAME with dynamic content from JavaScript

user256890 picture user256890 · Apr 19, 2010 · Viewed 14.9k times · Source

I have an IFRAME that should be filled with content from JavaScript. Had the content be on the server all I had to do is:

    function onIFrameFill() {
         myIframe.location.href = "HelloWorld.html";
     }

But the content I have is a HTML page generated on the client and represented as a string (I have not much influence on it). How can I populate the content of the my iframe programatically?

Answer

Jeffery To picture Jeffery To · Apr 19, 2010

I think you're looking for something like:

var iframeDoc = myIframe.contentWindow.document;
iframeDoc.open();
iframeDoc.write('hello world');
iframeDoc.close();