gives Error: Uncaught TypeError: Cannot read property ‘body’ of undefined
JavaScript
x
4
1
var f = document.createElement("iframe");
2
f.id = "s";
3
f.contentWindow.document.body.innerHTML = "body";
4
how can i fix this?
Advertisement
Answer
to add content to iframe, you should pass by src attribute, here is an example.
JavaScript
1
4
1
var iframe = document.createElement('iframe');
2
var html = '<body>Foo</body>';
3
iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html); document.body.appendChild(iframe);
4
if your content is url, you put it directly into src,like this
JavaScript
1
2
1
iframe.src="https://www.w3schools.com"
2