Skip to content

What does document write function do

  • by

JavaScript document write() function do write to the document stream, calling document.write on a closed (loaded) document automatically calls document.open, which will clear the document.

write(markup)

In general, rather than using document.write to add content to a page, use the various DOM methods.

Note: The write() method deletes all existing HTML when used on a loaded document.

For example, the document write function

A simple example code writes some text directly to the HTML output:

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

Output:

What does document write function do

Write some HTML elements directly to the HTML output:

document.write("<h2>Hello World!</h2><p>Have a nice day!</p>");

Do comment if you have any doubts or suggestions on this Js document topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *