W3cubDocs

/Web APIs

document: writeln() method

Warning: Use of the document.writeln() method is strongly discouraged.

As the HTML spec itself warns:

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. Therefore, avoid using document.writeln() — and if possible, update any existing code that is still using it.

Writes a string of text followed by a newline character to a document.

Syntax

js

writeln(line)

Parameters

  • line is string containing a line of text.

Return value

None (undefined).

Examples

js

document.writeln("<p>enter password:</p>");

Notes

document.writeln is the same as document.write but adds a newline.

Note: document.writeln (like document.write) does not work in XHTML documents (you'll get a "Operation is not supported" (NS_ERROR_DOM_NOT_SUPPORTED_ERR) error on the error console). This is the case if opening a local file with a .xhtml file extension or for any document served with an application/xhtml+xml MIME type. More information is available in the W3C XHTML FAQ.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
writeln 64
1–64Only supported for HTMLDocument, not all Document objects.
12 1 4 51
≤12.1–51Only supported for HTMLDocument, not all Document objects.
11
1–11Only supported for HTMLDocument, not all Document objects.
64
4.4–64Only supported for HTMLDocument, not all Document objects.
64
18–64Only supported for HTMLDocument, not all Document objects.
4 47
≤12.1–47Only supported for HTMLDocument, not all Document objects.
11
1–11Only supported for HTMLDocument, not all Document objects.
9.0
1.0–9.0Only supported for HTMLDocument, not all Document objects.

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/writeln