-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
The document.write()
API blocks the HTML parser (or rather, <script>
blocks the HTML parser because the script might use document.write()
) and can cause significant slowness for users (in particular when writing a <script src>
which also calls document.write()
, and again multiple levels deep). It's also responsible for significant implementation complexity to support (nested invocations of the parser, the need for speculative parsing...).
The spec has this warning:
This method has very idiosyncratic behavior. In some cases, this method can affect the state of the HTML parser while the parser is running, resulting in a DOM that does not correspond to the source of the document (e.g. if the string written is the string "
<plaintext>
" or "<!--
"). In other cases, the call can clear the current page first, as if document.open() had been called. In yet more cases, the method is simply ignored, or throws an exception. Users agents are explicitly allowed to avoid executing script elements inserted via this method. And to make matters even worse, the exact behavior of this method can in some cases be dependent on network latency, which can lead to failures that are very hard to debug. For all these reasons, use of this method is strongly discouraged.
https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#document.write()
Yet, this API is not marked as an obsolete feature. Why not?
MDN doesn't specifically discourage the use of document.write()
: https://developer.mozilla.org/en-US/docs/Web/API/Document/write
I'm aware of the following issue that could be argued should block obsoleting document.write()
:
- Consider adding
blocking=render
for dynamicimport()
#7976 - A way to stream content into an element #2142
Are there any other reasons?
(Note that I'm not suggesting that support should be removed. That would not be web-compatible.)
cc @whatwg/html-parser