I'm trying to create an iframe from JavaScript and fill it with arbitrary HTML, like so:
var html = '<body>Foo</body>';
var iframe = document.createElement('iframe');
iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
I would expect iframe
to then contain a valid window and document. However, this isn't the case:
> console.log(iframe.contentWindow);
null
Try it for yourself: http://jsfiddle.net/TrevorBurnham/9k9Pe/
What am I overlooking?
Thanks for your great question, this has caught me out a few times. When using dataURI HTML source, I find that I have to define a complete HTML document.
See below a modified example.
take note of the html content wrapped with
<html>
tags and theiframe.src
string.The iframe element needs to be added to the DOM tree to be parsed.
You will not be able to inspect the
iframe.contentDocument
unless youdisable-web-security
on your browser. You'll get a messageThere is an alternative for creating an iframe whose contents are a string of HTML: the srcdoc attribute. This is not supported in older browsers (chief among them: Internet Explorer, and possibly Safari?), but there is a polyfill for this behavior, which you could put in conditional comments for IE, or use something like has.js to conditionally lazy load it.
Do this
getIframeWindow is defined here
Setting the
src
of a newly creatediframe
in javascript does not trigger the HTML parser until the element is inserted into the document. The HTML is then updated and the HTML parser will be invoked and process the attribute as expected.http://jsfiddle.net/9k9Pe/2/
Also this answer your question it's important to note that this approach has compatibility issues with some browsers, please see the answer of @mschr for a cross-browser solution.
Allthough your
src = encodeURI
should work, I would have gone a different way:As this has no x-domain restraints and is completely done via the
iframe
handle, you may access and manipulate the contents of the frame later on. All you need to make sure of is, that the contents have been rendered, which will (depending on browser type) start during/after the .write command is issued - but not nescessarily done whenclose()
is called.A 100% compatible way of doing a callback could be this approach:
Iframes has the onload event, however. Here is an approach to access the inner html as DOM (js):