如何替换窗口中的文档?

发布于 2024-12-23 15:01:30 字数 367 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆依然 2024-12-30 15:01:30

您无法将当前文档对象或任何文档对象替换为通过 createHTMLDocument 方法创建的 Document 对象。

createHTMLDocument 首次在 DOM Level 2 Core 的一份草案中引入,但后来从最终建议中删除。

后来它被添加到 HTML5 规范中,因为没有编程方式来创建 HTML 文档。

为以编程方式创建 HTML 文档而提供的一些用例是:

  • 创建一个非渲染的 HTML 文档以通过 XMLHttpRequest 上传(而不是
    发送 XML 文档)。

  • 以保证的方式对库代码中的 HTML DOM 进行功能测试
    避免对显示的文档产生副作用。

  • 从富文本编辑区域创建一个隔离的非渲染文档,因此
    上传前可进行客户端清理,不影响直播
    用户仍可以进一步编辑 DOM。

  • 在 JavaScript 中实现 HTML5 解析算法客户端以进行测试和
    比较目的,或用于虚拟化或基于对象功能的安全性。

    不可见的 iframe 可用于大多数这些目的,但更多的是
    就资源而言是昂贵的。
    W3C 邮件列表

W3C 邮件列表上的对话将该方法带回到规范中,[Bug 7842] 新内容:没有以编程方式制作 HTML 文档 -考虑添加 createHTMLDocument

You cannot replace the current document object or any document object with the Document object created with createHTMLDocument method.

The createHTMLDocument was first introduced in one of the drafts of the DOM 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,

  • Create a non-rendered HTML document to upload via XMLHttpRequest (instead of
    sending an XML document).

  • Feature-test the HTML DOM in library code in a way that is guaranteed to
    avoid side effects on the displayed document.

  • Create an isolated non-rendered document from a rich text editing area, so
    client-side cleanup can be done before uploading without disturbing the live
    DOM that the user may still edit further.

  • Implement HTML5 parsing algorithm client-side in JavaScript for testing and
    comparison purposes, or for virtualization or object-capability-based security.

    An invisible iframe can be used for most of these purposes but that is more
    expensive in terms of resources.
    W3C mailing list

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

遗弃M 2024-12-30 15:01:30

document 中的某些内容与它包含的 DOM 树并不真正相关,
例如 document.cookielocationURL。如果我们不能替换像 windowdocument 这样的全局对象,那就更安全了。

但是,通过将主 documentdocumentElement 替换为其他 documentdocumentElement< ,可以有效地实现您正在寻找的内容。 /代码>。
它将具有与您正在寻找的效果完全相同的效果。*

document.replaceChild(
    document.importNode(newdoc.documentElement, true),
    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 and URL. It's much safer if we cannot replace global objects like window and document.

But what you are looking for can be effectively achieved by replacing the main document's documentElement with the other document's documentElement.
It will have exactly the same effect that you are looking for.*

document.replaceChild(
    document.importNode(newdoc.documentElement, true),
    document.documentElement
);

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.

清风夜微凉 2024-12-30 15:01:30

如果将文档序列化为 HTML,则可以使用 document.opendocument.writedocument.close 替换当前页面的文档。

事实上,您甚至可以通过添加 将 Quirks 模式更改为标准模式。

例如: http://jsbin.com/anusul/2

<html>    
    <script>
        alert('now in compatMode '+document.compatMode);
        if (document.compatMode==='BackCompat') {
            setTimeout(function() {
        var markup= '<!DOCTYPE html>New Page';
                document.open();
                document.write(markup);
                document.close();
            }, 0);
        }
    </script>
</html>​​​​​​

我不建议这样使用它没有特殊情况的欺骗,但它确实有效。

来源: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 and document.close.

In fact you can even change Quirks mode to standard mode by adding a <!doctype html>.

For example: http://jsbin.com/anusul/2

<html>    
    <script>
        alert('now in compatMode '+document.compatMode);
        if (document.compatMode==='BackCompat') {
            setTimeout(function() {
        var markup= '<!DOCTYPE html>New Page';
                document.open();
                document.write(markup);
                document.close();
            }, 0);
        }
    </script>
</html>​​​​​​

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文