About Lesson
Writing into the HTML Output: Using document.write()
The document.write() method is a simple way to directly write content into the HTML document as it loads. However, it should generally be avoided in modern web development because it can overwrite the entire HTML document if used after the page has finished loading.
Example:
JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>document.write() Example</title>
<script>
document.write("<h1>Hello, this is written using document.write()</h1>");
</script>
</head>
<body>
<p>This will be overwritten by document.write().</p>
</body>
</html>
- Usage: Useful for testing or injecting content during page load but not recommended for production sites.
- Consideration: If document.write() is executed after the page load, it will replace the entire page content.