How do I get the HTML source from the page?

Is there a way to access the page HTML source code using javascript?

I know that I can use document.body.innerHTML but it contains only the code inside the body. I want to get all the page source code including head and body tags with their content, and, if it's possible, also the html tag and the doctype. Is it possible?


Solution 1:

Use

document.documentElement.outerHTML

or

document.documentElement.innerHTML

Solution 2:

This can be done in a one-liner using XMLSerializer.

var generatedSource = new XMLSerializer().serializeToString(document);

Which gives String

<!DOCTYPE html><html><head>

<title>html - javascript page source code - Stack Overflow</title>
...

Solution 3:

One way to do this would be to re-request the page using XMLHttpRequest, then you'll get the entire page verbatim from the web server.

Solution 4:

Provided that

  • true html source code is wanted (not current DOM serization)
  • and that the page was loaded using GET method,

the page source can be re-downloaded:

fetch(document.location.href)
    .then(response => response.text())
    .then(pageSource => /* ... */)

Solution 5:

For IE you can also use: document.all[0].outerHTML