From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/document/open/index.html | 126 +++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 files/zh-cn/web/api/document/open/index.html (limited to 'files/zh-cn/web/api/document/open') diff --git a/files/zh-cn/web/api/document/open/index.html b/files/zh-cn/web/api/document/open/index.html new file mode 100644 index 0000000000..a6d75efaca --- /dev/null +++ b/files/zh-cn/web/api/document/open/index.html @@ -0,0 +1,126 @@ +--- +title: Document.open() +slug: Web/API/Document/open +tags: + - DOM + - Document + - Document.open() +translation_of: Web/API/Document/open +--- +

{{APIRef("DOM")}}

+ +

Document.open() 方法打开一个要写入的文档。

+ +

这将会有一些连带的影响。例如:

+ + + +

语法

+ +
document.open();
+
+ +

参数

+ +

无。

+ +

返回值

+ +

一个 Document 对象实例。

+ +

示例

+ +

以下简单的代码,会打开一个文档,并将原有内容替换为一些不同的HTML片段,然后关闭文档。

+ +
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();
+ +

注意

+ +

document.write() 在页面加载后调用,会发生自动的 document.open()调用。

+ +

很多年以来,Firefox和IE浏览器会在清除所有节点的同时,将所有Javascript变量等一并清除,但现在已经不采用这一做法。
+ document non-spec'ed parameters to document.open

+ +

不要和 window.open() 方法混淆。document.open 可用于重写当前的文档内容或者追加内容, 而 window.open 是提供了打开一个新的窗口的方法,当前的网页文档内容会被保留。由于 window 是一个全局对象,直接调用 open(...)  和 window.open(...) 的效果是一样的。你可以使用 document.close()关闭打开的文档。

+ +

See Security check basics for more about principals.

+ +

如果不想在当前文本追加内容, 使用 open("text/html", "replace") 替换 open() .

+ +

针对Gecko的注意事项

+ +

从Gecko 1.9开始,这个方法与其他属性一样受到同源策略的控制,若调用会使文档的源产生变化则不可用。

+ +

从Gecko 1.9.2开始,document.open() 使用文档的使用的URI的principal大,而不是从stack中取来principal。因此,你无需再在不可信的文档里调用 {{domxref("document.write()")}} ,包括使用wrappedJSObject。关于principal的更多信息详见Security check basics

+ + + +

三个参数的document.open()

+ +

有一个更少人知道且更少被使用的 document.open() 的版本,这是{{domxref("Window.open()")}} 的一个别名(前往该页面查看更多)。

+ +

这种调用,例如在新窗口打开github.com,把opener设为null

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

两个参数的document.open()

+ +

浏览器过往支持一个两个参数版本的document.open(),方法参数签名如下:

+ +
document.open(type, replace)
+
+ +

type指定了所需写入的数据的MIME类型,replace(如有设置,值为一个字符串“replace”)指定了新文档的历史写入会代替现有的例如写入。

+ +

这种形式现在已经弃用;它不会抛出错误,但会直接调用document.open()(相当于无参数形式的调用)。这种历史写入替换行为现在一定会发生。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName("HTML WHATWG", "#dom-document-open", "document.open()")}}{{Spec2("HTML WHATWG")}}
{{SpecName("DOM2 HTML", "html.html#ID-72161170", "document.open()")}}{{Spec2("DOM2 HTML")}}
+ + + +

浏览器兼容性

+ + + +

{{Compat("api.Document.open")}}

+ +

参见

+ + + + -- cgit v1.2.3-54-g00ecf