I have the following javascript:
var iframe = document.createElement('iframe');
iframe.src='javascript:""';
document.body.appendChild(iframe);
var doc = iframe.contentDocument;
doc.open();
doc.write('<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />');
doc.write('<link type="text/css" rel="stylesheet" href="http://cdn.sstatic.net/stackoverflow/all.css?v=db16ef7a3fac" />');
doc.write('<script type="text/javascript">;</' + 'script>');
doc.write('<title>test</title></head><body><br>test</body></html>');
doc.close();
console.log(doc.body);
setTimeout(function() {
console.log(doc.body);
}, 1000);
jsfiddle, open your console first
If I execute it in Firefox, it dumps <body>
properly two times.
If I execute it in Chrome, it displays first null
, then <body>
. However, If I execute it then again, Chrome also displays <body>
twice.
If you look at doc.childNodes
when body
is null
, you see that chrome has somehow stopped processing the HTML string right after the </script>
, therefore the body is not set.
The problem only occurs if the <link>
tag is present. If I comment out this line, it works properly.
Can you reproduce the problem with chrome? Is there a way to force Chrome to synchronously process the HTML string?
Edit: I just fixed the fiddle, it had the css line commented out.