Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

HTML | DOM write() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The write() method in HTML is used to write some content or JavaScript code in a Document. This method is mostly used for testing purposes. It is used to delete all the content from the HTML document and inserts the new content. It is also used to give additional text to an output that is opened by the document.open() method. This method is quite similar to writeln() method.

Syntax:

document.write( exp1, exp2, exp3, ... )

Parameters: This method contains many parameters which are optional. All the expression arguments (exp1, exp2, … ) can be listed and displayed in the order of occurrence. 

Example 1: This simple example shows the use of document.write() method.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM write() Method</title>
    <style>
        body {
            text-align:center;
        }
        h1 {
            color:green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM write() Method</h2>
    <script>
        document.write("GeeksforGeeks! ");
        document.write
            ("A computer science portal for geeks")
    </script>
</body>
 
</html>

Output:

  

Example 2: This is another example that shows the use of the document.write() method.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM write() Method</title>
    <style>
        body {
            text-align:center;
        }
        h1 {
            color:green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM write() Method</h2>
    <button type="button" onclick="Geeks()">
        Submit
    </button>
    <script>
        function Geeks() {
            document.write
                ('<center>computer science portal</center>');
        }
    </script>
</body>
 
</html>

Output:

 

Supported Browsers: The browser supported by the DOM write() method are listed below:

  • Chrome 1
  • Edge 12
  • Internet Explorer 4
  • Firefox 1
  • Opera 3
  • Safari 1

My Personal Notes arrow_drop_up
Last Updated : 10 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials