Jeff Meatball Yang Jeff Meatball Yang - 4 months ago 28x
jQuery Question

Write elements into a child iframe using Javascript or jQuery

I have something like this:

<iframe id="someFrame"></iframe>

And I would like to use jQuery to write elements such that the full equivalent HTML would be like this:

<iframe id="someFrame">
<!-- inside the iframe's content -->
<!-- <html><body> -->
<!-- </body></html> -->

Alternatively, any plain-old-Javascript would be fine.


Edit: After a little more research, it seems I am looking for an IE-equivalent of the contentDocument property of an iframe. "contentDocument" is a W3C standard which FF supports, but IE does not. (surprise surprise)


You can do both, you just have to target differently:

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