Filling an IFRAME with dynamic content from JavaSc

2019-01-17 12:55发布

I have an IFRAME that should be filled with content from JavaScript. Had the content be on the server all I had to do is:

    function onIFrameFill() {
         myIframe.location.href = "HelloWorld.html";
     }

But the content I have is a HTML page generated on the client and represented as a string (I have not much influence on it). How can I populate the content of the my iframe programatically?

3条回答
女痞
2楼-- · 2019-01-17 13:25

I think you're looking for something like:

var iframeDoc = myIframe.contentWindow.document;
iframeDoc.open();
iframeDoc.write('hello world');
iframeDoc.close();
查看更多
对你真心纯属浪费
3楼-- · 2019-01-17 13:28

What about .innerHTML?

myIframe.innerHTML = "This is some HTML <b>text</b>";
查看更多
祖国的老花朵
4楼-- · 2019-01-17 13:35

Tried setting .innerHTML but that does not work. Solution by Jeffery To works. Just want to add that myIframe.contentWindow might not work in old browsers (read IE old versions) so you can do

var iFrameWindow = myIframe.contentWindow || myIframe.documentWindow;
var iFrameDoc = iFrameWindow.document;

then use the document open(), write() & close() as above.

查看更多
登录 后发表回答