/

HTML / CSS / JavaScript Tutorial

JavaScript Document own method: write()

[this page | pdf | back links]

The write() method (when applied to the document object of the JavaScript DOM) writes HTML (which can include JavaScript code) to the document.

 

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 has the following syntax with the following parameters. It does not return any value.

 

document.write(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 isSupportedJavaScriptMethodDomWrite() {
  return !!document.write;
}


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


Desktop view | Switch to Mobile