如何替换窗口中的文档?
var newDoc = document.implementation.createHTMLDocument('someTitle');
// swap newDoc with document
DOMImplementation.createHTMLDocument
- 是否可以将当前文档替换为新文档?
- 这样做有什么合理的理由吗?
var newDoc = document.implementation.createHTMLDocument('someTitle');
// swap newDoc with document
DOMImplementation.createHTMLDocument
- Is it possible to swap the current document for a new document?
- Is there any reasonable reason to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法将当前文档对象或任何文档对象替换为通过
createHTMLDocument
方法创建的Document
对象。createHTMLDocument
首次在DOM Level 2 Core
的一份草案中引入,但后来从最终建议中删除。后来它被添加到 HTML5 规范中,因为没有编程方式来创建 HTML 文档。
为以编程方式创建 HTML 文档而提供的一些用例是:
W3C 邮件列表上的对话将该方法带回到规范中,[Bug 7842] 新内容:没有以编程方式制作 HTML 文档 -考虑添加 createHTMLDocument
You cannot replace the current document object or any document object with the
Document
object created withcreateHTMLDocument
method.The
createHTMLDocument
was first introduced in one of the drafts of theDOM Level 2 Core
, but was later removed from the final recommendation.It was later added to the HTML5 spec as there was no programmatic way to create an HTML document.
Some of the use cases provided for programmatic creation of an HTML document were,
The conversation on W3C mailing lists that brought the method back into the spec, [Bug 7842] New: No programmatic way to make an HTML document - consider adding createHTMLDocument
document
中的某些内容与它包含的 DOM 树并不真正相关,例如
document.cookie
、location
和URL
。如果我们不能替换像window
和document
这样的全局对象,那就更安全了。但是,通过将主
document
的documentElement
替换为其他document
的documentElement< ,可以有效地实现您正在寻找的内容。 /代码>。
它将具有与您正在寻找的效果完全相同的效果。*
至于这样做的原因,到目前为止我已经找到了一个 这是 iframe 无法实现的。
* 请注意,如果文档类型不同,则必须替换主文档的文档类型
节点与其他文档的 doctype 节点分开。
There is stuff in the
document
that is not really related to the DOM tree it contains,such as
document.cookie
,location
andURL
. It's much safer if we cannot replace global objects likewindow
anddocument
.But what you are looking for can be effectively achieved by replacing the main
document
'sdocumentElement
with the otherdocument
'sdocumentElement
.It will have exactly the same effect that you are looking for.*
As for reasons to do it, I have so far found one that cannot be achieved with an iframe.
* Note that if the doctypes are different, you'd have to replace the main document's doctype
node with the other document's doctype node separately.
如果将文档序列化为 HTML,则可以使用
document.open
、document.write
和document.close
替换当前页面的文档。事实上,您甚至可以通过添加
将 Quirks 模式更改为标准模式。
例如: http://jsbin.com/anusul/2
我不建议这样使用它没有特殊情况的欺骗,但它确实有效。
来源:Javascript从怪异模式切换到标准 + 需要帮助:jquery 将 doctype 添加到 html
If you serialize the document to HTML you can replace the document of the current page with
document.open
,document.write
anddocument.close
.In fact you can even change Quirks mode to standard mode by adding a
<!doctype html>
.For example: http://jsbin.com/anusul/2
I wouldn't advise using it such trickery without a special case scenario, but it does work.
source: Javascript switch from quirksmode to standard + Need help with: jquery prepend doctype to html