javascript使用严格的doctype创建iframe [英] javascript create an iframe with strict doctype
本文介绍了javascript使用严格的doctype创建iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个可嵌入其他网站的小部件。小部件是使用 document.write()
创建的iframe,但我不知道如何使用javascript应用iframe doctype。
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.
这是我的代码:
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>");
iframe已创建,但我没有应用doctype。
有办法吗?
The iframe is created but I the doctype is not applied. is there a way to do this?
谢谢
推荐答案
为了写入 iframe
,您首先需要创建它,将其附加到文档,然后到达其中以获取句柄它的 contentDocument
。
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
.
以下是一些示例代码:
// 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);
看到它在这个 jsfiddle 。
这篇关于javascript使用严格的doctype创建iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文