Javascript – putting html inside an iframe (using javascript)


Can I create an empty iframe as a placeholder to later insert html into it?

In otherwords, suppose I have an empty iframe with an id,

How do I insert html in it?

I'm using jquery, if that makes it easier.

Best Answer

You can do it without jQuery also:

var iframe = document.getElementById('iframeID');
iframe = iframe.contentWindow || ( iframe.contentDocument.document || iframe.contentDocument);;
iframe.document.write('Hello World!');

jQuery's html strips body, html and head tags from the inserted HTML.