Sometimes, we want to get the entire document HTML as a string with JavaScript.
In this article, we’ll look at how to get the entire document HTML as a string with JavaScript.
How to get the entire document HTML as a string with JavaScript?
To get the entire document HTML as a string with JavaScript, we use the document.documentElement.innerHTML
property.
For instance, we write
const markup = document.documentElement.innerHTML;
console.log(markup);
to get the document object with document.documentElement
.
Then we use the innerHTML
property to return a string with the markup of the document.
Conclusion
To get the entire document HTML as a string with JavaScript, we use the document.documentElement.innerHTML
property.