简体   繁体   中英

How to replace entire contents of iframe?

This question didn't receive any satisfactory answers, so I'm asking again.

I can get my iframe like this:

var iframe = document.getElementById('preview');
var win = iframe.contentWindow;
var doc = win.document;

And write some initial HTML to it like this:

doc.write('<!DOCTYPE html><html><head><link rel="stylesheet" type="text/css" href="reset.css"></head><body>blah blah</body></html>');

And that works great, but now I want to replace the entire thing with some new content. How can I do that?

To be clear, by "entire thing" I mean the <head> too. I need to link some external JS and CSS there.

Preferably, this would also include the <!DOCTYPE> , but it's non-essential at this point.

您只需从DOM中删除旧的iframe节点,然后创建一个包含新内容的新节点。

我相信使用innerHTML应该有效:

doc.documentElement.innerHTML = "<script src='blah.js'></script><style></style>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM