Document.open() - Web APIs 编辑

The Document.open() method opens a document for writing.

This does come with some side effects. For example:

  • All event listeners currently registered on the document, nodes inside the document, or the document's window are removed.
  • All existing nodes are removed from the document.

Syntax

document.open();

Parameters

None.

Return value

A Document object instance.

Examples

The following simple code opens the document and replaces its content with a number of different HTML fragments, before closing it again.

document.open();
document.write("<p>Hello world!</p>");
document.write("<p>I am a fish</p>");
document.write("<p>The number is 42</p>");
document.close();

Notes

An automatic document.open() call happens when document.write() is called after the page has loaded.

For years Firefox and Internet Explorer additionally erased all JavaScript variables, etc., in addition to removing all nodes. This is no longer the case.document non-spec'ed parameters to document.open

Gecko-specific notes

Starting with Gecko 1.9, this method is subject to the same same-origin policy as other properties, and does not work if doing so would change the document's origin.

Starting with Gecko 1.9.2, document.open() uses the principal of the document whose URI it uses, instead of fetching the principal off the stack. As a result, you can no longer call document.write() into an untrusted document from chrome, even using wrappedJSObject. See Security check basics for more about principals.

Three-argument document.open()

There is a lesser-known and little-used three-argument version of document.open() , which is an alias of Window.open() (see its page for full details).

This call, for example opens github.com in a new window, with its opener set to null:

document.open('https://www.github.com','', 'noopener=true')

Two-argument document.open()

Browsers used to support a two-argument document.open(), with the following signature:

document.open(type, replace)

Where type specified the MIME type of the data you are writing (e.g. text/html) and replace if set (i.e. a string of "replace") specified that the history entry for the new document would replace the current history entry of the document being written to.

This form is now obsolete; it won't throw an error, but instead just forwards to document.open() (i.e. is the equivalent of just running it with no arguments).  The history-replacement behavior now always happens.

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'document.open()' in that specification.
Living Standard
Document Object Model (DOM) Level 2 HTML Specification
The definition of 'document.open()' in that specification.
Obsolete

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:83 次

字数:5608

最后编辑:7 年前

编辑次数:0 次

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