
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to get the entire document HTML as a string in JavaScript?
One of the most useful features of JavaScript is the ability to get an entire document's HTML as a string. This can be used for many purposes, such as obtaining data from a website or creating dynamic content on your own website. In this article, we'll go over how to get the entire document HTML as a string in JavaScript.
To get the entire document HTML as a string, use the concept of innerHTML
The dynamic html can be written on the html document using the innerHTML property. The majority of the time, it is used in web pages to create dynamic html for things like comment forms, links, and registration forms.
The getElementsByTagName() Method
This function returns a NodeList object that contains a list of all the elements in the document that have the specified tag name. This object represents a group of nodes that can be reached via index numbers. The index begins at zero.
Syntax
Following is the syntax for getElementsByTagName()
document.getElementsByTagName(tagname)
HTML DOM innerHTML Property
The innerHTML property sets or returns the HTML content (inner HTML) of an element.
Syntax
Return the innerHTML property
Following is the syntax for return innerHTML property
HTMLElementObject.innerHTML
Set the innerHTML property
Following is the syntax of set the innerHTML property
HTMLElementObject.innerHTML = text
Let’s dive into the following examples to understand more about conversion of whole document into string.
Example
In the following example the scripts get the entire document as string by using document.documentElement.innerHTML.
<!DOCTYPE html> <html> <body style="text-align:center;" id="body"> <p id="tutorial" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="tutorial1(); "> click here </button> <script> var up = document.getElementById('tutorial'); up.innerHTML = 'Click To Convert Whole Document To String'; function tutorial1() { var string = document.documentElement.innerHTML; alert(string); } </script> </body> </html>
When the script gets executed, it will generate an output consisting of text along with a button displayed on the webpage. If the user clicks on the button, the event gets triggered, converts the whole document into a string, and displays it as an alert.
Example
Considering the following example, it gets the whole document by first selecting element with tagname “HTML” and selecting the first element by indexing using. document.getElementsByTagName(‘html’)[0].innerHTML.
<!DOCTYPE html> <html> <body style="text-align:center;" id="body"> <p id="tutorial" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="tutorial1(); "> click here </button> <script> var up = document.getElementById('tutorial'); up.innerHTML = 'Click on the button to convert whole document to string'; function tutorial1() { var string = document.getElementsByTagName('html')[0].innerHTML; alert(string); } </script> </body> </html>
On running the above script, the web- browser displays the button along with some text on the webpage. When the user clicks the button, the event gets triggered, which converts the whole document into a string and displays it as an alert.
- Related Articles
- How do you update a MongoDB document while replacing the entire document?
- How to get the cookies associated with a document in JavaScript?
- How to get a particular anchor in a document in JavaScript?
- How to get the number of forms in a document with JavaScript?
- How to get the title and full URL of a document in JavaScript?
- How to render the remainder of the document as preformatted plain text in HTML?
- Create HTML Document with Custom URL for the document in JavaScript
- How to get the length of a string in JavaScript?
- How to get a variable name as a string in Python?
- How to get a function name as a string in Python?
- How to get a variable name as a string in PHP?
- How to create a section in a document in HTML?
- How an entire file is read into buffer and returned as a string in Python?
- How to remove html tags from a string in JavaScript?
- How to get the length of a string in bytes in JavaScript?
