Using Document.Write()

For testing purposes, it is convenient to use document.write():

HTML file: Displayed by browser:
<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<script>
document.write(5 + 6);
</script>

</body>
</html>

My First Web Page

My first paragraph.


Using document.write() after an HTML document is fully loaded, will delete all existing HTML:

HTML file: Displayed by browser:
<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<button onclick="document.write(5 + 6)">Lose everything but the 11!</button>

</body>
</html>

My First Web Page

My first paragraph.

The document.write() method should be used only for testing.

BackTable of ContentsNext