javascript使用严格的doctype创建iframe

前端之家收集整理的这篇文章主要介绍了javascript使用严格的doctype创建iframe前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个可嵌入其他站点的小部件.小部件是使用document.write()创建的iframe,但我不知道如何使用 javascript应用iframe文档类型.

这是我的代码

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.
有没有办法做到这一点?

谢谢

解决方法@H_403_13@
为了写入iframe,您首先需要创建它,将其附加到文档,然后到达其内部以获取其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相关文章