HTML | DOM write() Method

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

Syntax:

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

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                            

chevron_right


Output:

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

  • Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.