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
For IE you can also use: document.all[0].outerHTML
Use
document.documentElement.outerHTML
or
document.documentElement.innerHTML
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.
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>
...
Provided that
the page source can be re-downloaded:
fetch(document.location.href)
.then(response => response.text())
.then(pageSource => /* ... */)