Open In App

How to get the entire HTML document as a string in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are given an HTML document, the task is to get the entire document as a string using JavaScript.

Methods to get an Entire HTML document as a String:

Method 1: JavaScript getElementsByTagName() Method

This method returns a set of all elements in the document with the defined tag name, as a NodeList object. This object represents a collection of nodes, Which are accessed by index numbers. The index starts at 0. 

Syntax: 

document.getElementsByTagName(tagname);

Example: This example gets the whole document by first selecting the elements with the tag name ‘HTML’ and selecting the first element by indexing using document.getElementsByTagName(‘html’)[0].innerHTML.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        How to get the entire HTML document as a string
        in JavaScript ?
    </title>
</head>
 
<body style="text-align: center" id="body">
    <h1 style="color: green">GeeksforGeeks</h1>
    <h3>
        JavaScript | Get the entire document HTML as a
        string.
    </h3>
    <p id="GFG_UP" style="font-size: 15px; font-weight: bold"></p>
    <button onclick="GFG_Fun(); ">click here</button>
    <script>
        let up = document.getElementById("GFG_UP");
        up.innerHTML =
            "Click on the button to convert whole document to string";
 
        function GFG_Fun() {
            let string =
                document.getElementsByTagName("html")[0]
                    .innerHTML;
            alert(string);
        }
    </script>
</body>
 
</html>


Output: 

Get the entire HTML document as a string

Get the entire HTML document as a string

Method 2: HTML DOM innerHTML Property

This property set/returns the HTML content (inner HTML) of an element. 

Syntax: 

HTMLElementObject.innerHTML

Example: This example gets the whole document as a string using document.documentElement.innerHTML

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        How to get the entire HTML document as a string
        in JavaScript ?
    </title>
</head>
 
<body style="text-align: center" id="body">
    <h1 style="color: green">GeeksforGeeks</h1>
    <h3>
        JavaScript | Get the entire document HTML as a
        string.
    </h3>
    <p id="GFG_UP" style="font-size: 15px; font-weight: bold"></p>
    <button onclick="GFG_Fun(); ">click here</button>
    <script>
        let up = document.getElementById("GFG_UP");
        up.innerHTML =
            "Click on the button to convert whole document to string";
 
        function GFG_Fun() {
            let string =
                document.documentElement.innerHTML;
            alert(string);
        }
    </script>
</body>
 
</html>


Output: 

Get the entire HTML document as a string

Get the entire HTML document as a string



Last Updated : 13 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads