/

HTML / CSS / JavaScript Tutorial

JavaScript Document own method: writeln()

[this page | pdf | back links]

The writeln() method (when applied to the document object of the JavaScript DOM) writes HTML (which can include JavaScript code) to the document, writing a newline character after each expression.

 

It is mostly used for testing, as if it is used after an HTML document is fully loaded it will delete the existing HTML. Alternatively, it may be used to write to a bespoke output stream opened by the document.open() method.

 

It is essentially the same as document.write() except that it adds a new line character after each statement.

 

It has the following syntax with the following parameters. It does not return any value.

 

document.writeln(expr1, expr2, expr3, )

 

Parameter

Required / Optional

Description

expr1, expr2, expr3,

Optional

Text written to output stream

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>

<script>
document.write("Hello World!");
document.write("Hello World!");

document.writeln("Hello World2!");
document.writeln("Hello World2!");
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodDomWriteln() {
  return !!document.writeln;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile