最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript create an iframe with strict doctype - Stack Overflow

programmeradmin2浏览0评论

I'm building a widget that can be embedded in other sites. The widget is an iframe that is created using document.write() however I don't know how to apply the iframe doctype using javascript.

Here is my code:

document.write("<iframe scrolling=\"no\" frameborder=\"0\">");
document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"   \".dtd\">");
document.write("<html>");
document.write("<head></head><body></body>");
document.write("</html>");
document.write("</iframe>");
document.write("</div>");

The iframe is created but I the doctype is not applied. is there a way to do this?

Thanks

I'm building a widget that can be embedded in other sites. The widget is an iframe that is created using document.write() however I don't know how to apply the iframe doctype using javascript.

Here is my code:

document.write("<iframe scrolling=\"no\" frameborder=\"0\">");
document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"   \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
document.write("<html>");
document.write("<head></head><body></body>");
document.write("</html>");
document.write("</iframe>");
document.write("</div>");

The iframe is created but I the doctype is not applied. is there a way to do this?

Thanks

Share Improve this question asked Jan 6, 2013 at 19:52 Doua BeriDoua Beri 10.9k18 gold badges94 silver badges143 bronze badges 4
  • 1 I'm afraid this is not really working. All elements between iframe tags are in the original page, and are shown, if the browser doesn't support iframes. What you get with this code is only an iframe with src="about:blank". Please check this: MDN: <iframe>. – Teemu Commented Jan 6, 2013 at 20:00
  • @Teemu oh.. I see.. but the question still remains. I created an inframe with javascript and I can add elements to iframe using javascript. is there a way to add doctype? – Doua Beri Commented Jan 6, 2013 at 20:15
  • If you mean, that you really "document.write()" some elements between body tags in your code, and they also can be seen in the iframe, this question would be beyond my knowledge : ). – Teemu Commented Jan 7, 2013 at 9:09
  • This post might also be interesting reading... – Teemu Commented Jan 7, 2013 at 18:00
Add a comment  | 

2 Answers 2

Reset to default 16

In order to write into the iframe, you'll first need to create it, attach it to the document, and then reach inside of it to get a handle on its contentDocument.

Here is some sample code:

// create the iframe and attach it to the document
var iframe = document.createElement("iframe");
iframe.setAttribute("scrolling", "no");
iframe.setAttribute("frameborder", "0");
document.body.appendChild(iframe);

// find the iframe's document and write some content
var idocument = iframe.contentDocument;
idocument.open();
idocument.write("<!DOCTYPE html>");
idocument.write("<html>");
idocument.write("<head></head>");
idocument.write("<body>this is the iframe</body>");
idocument.write("</html>");
idocument.close();

// now have a look at your creation in the console
console.log(idocument);

See it working in this jsfiddle.

You could also use an HTML5 srcdoc attribute to specify your iframe's content.

document.getElementById('myFrame').srcdoc = "<!DOCTYPE html PUBLIC....";

You'll have to check if srcdoc is supported by your browser.

发布评论

评论列表(0)

  1. 暂无评论