Document:write() 方法

警告: 强烈不建议使用 document.write() 方法。

正如 HTML 规范本身所警告的

这种方法具有非常特殊的行为。在某些情况下,当解析器运行时,此方法可能会影响 HTML 解析器的状态,导致生成的 DOM 与文档源代码不符(例如,如果写入的字符串为“<plaintext>”或者“<!--”)。其他情况下,该调用可能会先清除当前页面,就像调用了 document.open() 一样。更多情况下,该方法会被直接忽略或抛出异常。用户代理被明确允许避免执行通过此方法插入的 script 元素。更糟糕的是,这种方法的确切行为在某些情况下取决于网络延迟,这可能导致难以调试的故障。鉴于上述原因,我们强烈反对使用这种方法。因此,避免使用 document.write()——如果允许的话,更新仍在使用它的任何现有代码。

document.write() 方法将文本字符串写入由 document.open() 打开的文档流。

备注: 因为 document.write() 会向文档中写入内容,所以在已关闭(已加载)的文档上调用 document.write() 会自动调用 document.open()这将清空文档

语法

js
write(markup)

参数

markup

包含要写入文档的文本的字符串。

返回值

无(undefined)。

示例

html
<html lang="zh-CN">
  <head>
    <title>编写示例</title>

    <script>
      function newContent() {
        document.open();
        document.write("<h1>旧的不去,新的不来!</h1>");
        document.close();
      }
    </script>
  </head>

  <body onload="newContent();">
    <p>一些原始文档内容。</p>
  </body>
</html>

备注

你编写的文本会被解析为文档结构模型。在上面示例中,h1 元素成为文档中的一个节点。

在未调用 document.open() 的情况下,向已加载的文档写入内容将自动调用 document.open()。写入后,调用 document.close() 告诉浏览器完成页面加载。

如果 document.write() 调用嵌入了行级 HTML <script> 标签,则不会调用 document.open()。例如:

html
<script>
  document.write("<h1>主标题</h1>");
</script>

备注: document.write()document.writeln 在 XHTML 文档中无法正常工作(你会在控制台收到“Operation is not supported”(NS_ERROR_DOM_NOT_SUPPORTED_ERR)的错误)。当以 .xhtml 文件扩展名打开本地文件或任何使用 application/xhtml+xml MIME 类型的文档时,就会出现这种情况。更多详细信息,请参阅 W3C XHTML 常见问题解答

备注:延迟异步脚本中使用 document.write() 将被忽略,并且你会在错误控制台中收到类似于“A call to document.write() from an asynchronously-loaded external script was ignored”的消息。

备注: 仅在 Edge 中,在 <iframe> 中多次调用 document.write() 会导致错误“SCRIPT70: Permission denied”。

备注: 从 55 版本开始,当满足特定条件时,Chrome 将不会执行通过 <script> 元素中的 document.write()。有关详细信息,请参阅干预 document.write()

规范

Specification
HTML
# dom-document-write-dev

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
write
Deprecated

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Deprecated. Not for use in new websites.
Has more compatibility info.

参见