JavaScript's document.write() method is primarily used to output content to a web page. It dynamically generates HTML content that is inserted directly into the document at the location where the script is executed. This method is particularly useful for generating dynamic content, such as text, HTML elements, and even entire sections of a webpage, based on user interactions or data retrieved from external sources.


Syntax:
 

document.write(expression);
 

Here, expression can be a string containing HTML code, JavaScript variables, or any valid JavaScript expression that evaluates to a string.


Example 1: Writing Text Using document.write()

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document Write Example</title>
</head>
<body>
    <script>
        document.write("Hello, World!");
    </script>
</body>
</html>
 





Example 2: Writing HTML Elements Using document.write()

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document Write Example</title>
</head>
<body>
    <script>
        document.write("<h1>Welcome to My Website</h1>");
        document.write("<p>This is a paragraph of dynamic content.</p>");
    </script>
</body>
</html>
 





Advantages of document.write()
 
  • Dynamic Content: It allows for the creation of dynamic content based on user interactions or data.
  • Ease of Use: Writing HTML directly within JavaScript simplifies content generation.
  • Immediate Output: Content written using document.write() is displayed immediately as the script is executed.
  • Flexibility: Can be used to generate complex HTML structures, including elements, attributes, and styles.


Limitations of document.write()
  • Overwrites Content: If used after the document has finished loading, it replaces the entire content of the document.
  • Not Suitable for Modern Web Development: In modern web development, manipulating the DOM directly is preferred over document.write() for better control and performance.
  • Execution Timing: It should be used carefully, as executing document.write() after the document has loaded can cause issues with the document structure.


Best Practices
 
  • Use Early: Preferably use document.write() during page load or within <script> tags in the <head> section.
  • Avoid After Load: Avoid using document.write() after the document has finished loading to prevent content overwrite.
  • Sanitize Input: When writing user-generated content, ensure it is properly sanitized to prevent security vulnerabilities.


Example 3: Using document.write() for User Interaction

 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>User Interaction Example</title>
</head>
<body>
    <button onclick="showMessage()">Click Me</button>

    <script>
        function showMessage() {
            var name = prompt("Enter your name:");
            document.write("Hello, " + name + "! Welcome to our website.");
        }
    </script>
</body>
</html>



 

In this example, clicking the "Click Me" button prompts the user to enter their name. The entered name is then used with document.write() to display a personalized greeting message on the webpage.


 



Practice Excercise Practice now