Search code examples
javascripthtmldocumenttostring

How to get the entire document HTML as a string?


Is there a way in JS to get the entire HTML within the html tags, as a string?

document.documentElement.??

Solution

  • Get the root <html> element with document.documentElement then get its .innerHTML:

    const txt = document.documentElement.innerHTML;
    alert(txt);
    

    or its .outerHTML to get the <html> tag as well

    const txt = document.documentElement.outerHTML;
    alert(txt);