332

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

document.documentElement.??
6
  • 30
    The only correct answer: stackoverflow.com/questions/817218/… (stop up-voting inner/outerHTML answers, they do NOT provide the entire source!)
    – John
    Commented Dec 31, 2016 at 3:54
  • 5
    document.body.parentElement.innerHTML Commented Apr 16, 2018 at 16:17
  • @John what don't they provide? Commented Feb 4, 2020 at 1:37
  • 2
    Op did not ask for the entire source, please calm down John. Commented Mar 27, 2022 at 17:34
  • 6
    Stop upvoting John's bolded comment! The answer he links to replaces && with &amp;&amp; and so it breaks all your inline <script> tags! You should use document.documentElement.outerHTML instead, but note that it doesn't grab <!DOCTYPE html>, so you'll need to add that yourself.
    – joe
    Commented Oct 31, 2022 at 3:58

17 Answers 17

417

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);
4
  • 44
    outerHTML doesn't get the doctype. Commented Apr 10, 2014 at 2:50
  • 2
    worked like a charm! thank you! is there any way to get the size of any/all files linked to the document as well including js and css files?
    – www139
    Commented Mar 17, 2015 at 2:54
  • @CMCDragonkai: You could get the doctype separately and prepend it to the markup string. Not ideal, I know, but possible. Commented Nov 19, 2015 at 19:58
  • note that neither this nor none of these answers necessarily give you content that is the exact hash equivalent of saving the page to a file or the file generated by view-source. It seems the DOM normalizes some fields from the literal response content, like capitalising DOCTYPE headers
    – wesinat0r
    Commented Jul 7, 2020 at 0:51
144

You can do

new XMLSerializer().serializeToString(document)

in browsers newer than IE 9

See https://caniuse.com/xml-serializer

12
  • 10
    This was the first correct answer according to date/time stamps. Parts of the page such as the XML declaration will not be included and browsers will manipulate the code when using the other "answers". This is the only post that should be up-voted (dos's posted three days later). People need to pay attention!
    – John
    Commented Dec 31, 2016 at 3:57
  • 6
    This is not entirely correct since it serializeToString performs an HTML encode. For example if your code contains styles defining fonts such as "Times New Roman", Times, serif the quotes will get html encoded. Perhaps that is not important to some of you but to me it is...
    – Marko
    Commented Jun 6, 2017 at 20:43
  • 6
    @John well the OP actually asks for "the entire HTML within the html tags". And the selected best answer by Colin Burnett does achieve this. This particular answer (Erik's) will include the html tags and the doctype. That said, this was totally a diamond in the rough for me and exactly what I was looking for! Your comment helped too because it made me spend more time with this answer, so thanks :) Commented Oct 26, 2017 at 22:46
  • 5
    I think people should be careful with this one, specifically because it returns a value that is not the actual html that your browser receives. In my case, it added attributes to the html tag that the server never actually sent :(
    – onassar
    Commented Dec 23, 2018 at 19:58
  • 1
    It's supported in every browser. How is this poor browser support?
    – user187676
    Commented May 17, 2019 at 19:26
56

I tried the various answers to see what is returned. I'm using the latest version of Chrome.

The suggestion document.documentElement.innerHTML; returned <head> ... </body>

Gaby's suggestion document.getElementsByTagName('html')[0].innerHTML; returned the same.

The suggestion document.documentElement.outerHTML; returned <html><head> ... </body></html> which is everything apart from the 'doctype'.

You can retrieve the doctype object with document.doctype; This returns an object, not a string, so if you need to extract the details as strings for all doctypes up to and including HTML5 it is described here: Get DocType of an HTML as string with Javascript

I only wanted HTML5, so the following was enough for me to create the whole document:

alert('<!DOCTYPE HTML>' + '\n' + document.documentElement.outerHTML);

1
  • 9
    This is the most complete answer and should be accepted. As of 2016, browser compatibility is complete, and mentioning it in detail (as in the currently accepted answer) is no longer necessary. Commented Feb 17, 2016 at 19:45
51

I believe document.documentElement.outerHTML should return that for you.

According to MDN, outerHTML is supported in Firefox 11, Chrome 0.2, Internet Explorer 4.0, Opera 7, Safari 1.3, Android, Firefox Mobile 11, IE Mobile, Opera Mobile, and Safari Mobile. outerHTML is in the DOM Parsing and Serialization specification.

The MSDN page on the outerHTML property notes that it is supported in IE 5+. Colin's answer links to the W3C quirksmode page, which offers a good comparison of cross-browser compatibility (for other DOM features too).

9
  • Not all browsers support this. Commented May 3, 2009 at 14:38
  • @Colin: Yeah, good point. From experience, I seem to remember that both IE 6+ and Firefox support it, though the quirksmode page you linked suggests otherwise...
    – Noldorin
    Commented May 3, 2009 at 14:42
  • Firefox does not support OuterHTML. It is IE proprietary. developer.mozilla.org/En/… Commented May 3, 2009 at 14:53
  • 5
    Is there a way to get everything including the doctype and the html tags?
    – trusktr
    Commented Apr 10, 2012 at 21:36
  • 1
    Mine was first, actually. :P
    – Noldorin
    Commented Feb 17, 2016 at 19:43
11

You can also do:

document.getElementsByTagName('html')[0].innerHTML

You will not get the Doctype or html tag, but everything else...

7
document.documentElement.outerHTML
3
  • 1
    Not all browsers support this. Commented May 3, 2009 at 14:38
  • 2
    Supported in Firefox 11, Chrome 0.2, Internet Explorer 4.0, Opera 7, Safari 1.3, Android, Firefox Mobile 11, IE Mobile, Opera Mobile, and Safari Mobile (MDN). outerHTML is in the DOM Parsing and Serialization specification.
    – XP1
    Commented Dec 1, 2012 at 1:33
  • Colin's answer is more detailed. Commented Feb 17, 2016 at 19:43
6

PROBABLY ONLY IE:

>     webBrowser1.DocumentText

for FF up from 1.0:

//serialize current DOM-Tree incl. changes/edits to ss-variable
var ns = new XMLSerializer();
var ss= ns.serializeToString(document);
alert(ss.substr(0,300));

may work in FF. (Shows up the VERY FIRST 300 characters from the VERY beginning of source-text, mostly doctype-defs.)

BUT be aware, that the normal "Save As"-Dialog of FF MIGHT NOT save the current state of the page, rather the originallly loaded X/h/tml-source-text !! (a POST-up of ss to some temp-file and redirect to that might deliver a saveable source-text WITH the changes/edits prior made to it.)

Although FF surprises by good recovery on "back" and a NICE inclusion of states/values on "Save (as) ..." for input-like FIELDS, textarea etc. , not on elements in contenteditable/ designMode...

If NOT a xhtml- resp. xml-file (mime-type, NOT just filename-extension!), one may use document.open/write/close to SET the appr. content to the source-layer, that will be saved on user's save-dialog from the File/Save menue of FF. see: http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite resp.

https://developer.mozilla.org/en-US/docs/Web/API/document.write

Neutral to questions of X(ht)ML, try a "view-source:http://..." as the value of the src-attrib of an (script-made!?) iframe, - to access an iframes-document in FF:

<iframe-elementnode>.contentDocument, see google "mdn contentDocument" for appr. members, like 'textContent' for instance. 'Got that years ago and no like to crawl for it. If still of urgent need, mention this, that I got to dive in ...

3
document.documentElement.innerHTML
1
  • This doesn't return the <html ...> tag. Commented Feb 17, 2016 at 19:44
3

To also get things outside the <html>...</html>, most importantly the <!DOCTYPE ...> declaration, you could walk through document.childNodes, turning each into a string:

const html = [...document.childNodes]
    .map(node => nodeToString(node))
    .join('\n') // could use '' instead, but whitespace should not matter.

function nodeToString(node) {
    switch (node.nodeType) {
        case node.ELEMENT_NODE:
            return node.outerHTML
        case node.TEXT_NODE:
            // Text nodes should probably never be encountered, but handling them anyway.
            return node.textContent
        case node.COMMENT_NODE:
            return `<!--${node.textContent}-->`
        case node.DOCUMENT_TYPE_NODE:
            return doctypeToString(node)
        default:
            throw new TypeError(`Unexpected node type: ${node.nodeType}`)
    }
}

I published this code as document-outerhtml on npm.


edit Note the code above depends on a function doctypeToString; its implementation could be as follows (code below is published on npm as doctype-to-string):

function doctypeToString(doctype) {
    if (doctype === null) {
        return ''
    }
    // Checking with instanceof DocumentType might be neater, but how to get a
    // reference to DocumentType without assuming it to be available globally?
    // To play nice with custom DOM implementations, we resort to duck-typing.
    if (!doctype
        || doctype.nodeType !== doctype.DOCUMENT_TYPE_NODE
        || typeof doctype.name !== 'string'
        || typeof doctype.publicId !== 'string'
        || typeof doctype.systemId !== 'string'
    ) {
        throw new TypeError('Expected a DocumentType')
    }
    const doctypeString = `<!DOCTYPE ${doctype.name}`
        + (doctype.publicId ? ` PUBLIC "${doctype.publicId}"` : '')
        + (doctype.systemId
            ? (doctype.publicId ? `` : ` SYSTEM`) + ` "${doctype.systemId}"`
            : ``)
        + `>`
    return doctypeString
}

0
1

I always use

document.getElementsByTagName('html')[0].innerHTML

Probably not the right way but I can understand it when I see it.

1
  • This is incorrect because it won't return the <html...> tag. Commented Feb 17, 2016 at 19:47
1

I am using outerHTML for elements (the main <html> container), and XMLSerializer for anything else including <!DOCTYPE>, random comments outside the <html> container, or whatever else might be there. It seems that whitespace isn't preserved outside the <html> element, so I'm adding newlines by default with sep="\n".

function get_document_html(sep="\n") {
    let html = "";
    let xml = new XMLSerializer();
    for (let n of document.childNodes) {
        if (n.nodeType == Node.ELEMENT_NODE)
            html += n.outerHTML + sep;
        else
            html += xml.serializeToString(n) + sep;
    }
    return html;
}

console.log(get_document_html().slice(0, 200));

1

This would work if you want to get everything outside the DOCTYPE:

document.getElementsByTagName('html')[0].outerHTML;

or this if you want the doctype too:

new XMLSerializer().serializeToString(document.doctype) + document.getElementsByTagName('html')[0].outerHTML;
1

Using querySelector

const html = document.querySelector("html").outerHTML;
console.log(html)

0

I just need doctype html and should work fine in IE11, Edge and Chrome. I used below code it works fine.

function downloadPage(element, event) {
    var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);

    if ((navigator.userAgent.indexOf("MSIE") != -1) || (!!document.documentMode == true)) {
        document.execCommand('SaveAs', '1', 'page.html');
        event.preventDefault();
    } else {
        if(isChrome) {
            element.setAttribute('href','data:text/html;charset=UTF-8,'+encodeURIComponent('<!doctype html>' + document.documentElement.outerHTML));
        }
        element.setAttribute('download', 'page.html');
    }
}

and in your anchor tag use like this.

<a href="#" onclick="downloadPage(this,event);" download>Download entire page.</a>

Example

    function downloadPage(element, event) {
    	var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
    
    	if ((navigator.userAgent.indexOf("MSIE") != -1) || (!!document.documentMode == true)) {
    		document.execCommand('SaveAs', '1', 'page.html');
    		event.preventDefault();
    	} else {
    		if(isChrome) {
                element.setAttribute('href','data:text/html;charset=UTF-8,'+encodeURIComponent('<!doctype html>' + document.documentElement.outerHTML));
    		}
    		element.setAttribute('download', 'page.html');
    	}
    }
I just need doctype html and should work fine in IE11, Edge and Chrome. 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

<p>
<a href="#" onclick="downloadPage(this,event);"  download><h2>Download entire page.</h2></a></p>

<p>Some image here</p>

<p><img src="https://placeimg.com/250/150/animals"/></p>

-1

Use document.documentElement.

Same Question answered here: https://stackoverflow.com/a/7289396/2164160

1
  • That question should be closed as pretty much a duplicate of this one, which is much older. Anyway, the interesting part is that you need .outerHTML and to get the document.doctype, and the most complete answer is Paolo's. Commented Feb 17, 2016 at 19:53
-3

You have to iterate through the document childNodes and getting the outerHTML content.

in VBA it looks like this

For Each e In document.ChildNodes
    Put ff, , e.outerHTML & vbCrLf
Next e

using this, allows you to get all elements of the web page including < !DOCTYPE > node if it exists

-10

The correct way is actually:

webBrowser1.DocumentText

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.