This is a feature that has support in IE6 +, it does not use outerHTML for more support, adds doctype, and uses a few tricks to get the html tag and its attributes. To get a line with doctype and not using outerHTML , so it supports every browser. It uses a few tricks to get the html tag. Add this code:
document.fullHTML = function () { var r = document.documentElement.innerHTML, t = document.documentElement.attributes, i = 0, l = '', d = '<!DOCTYPE ' + document.doctype.name + (document.doctype.publicId ? ' PUBLIC "' + document.doctype.publicId + '"' : '') + (!document.doctype.publicId && document.doctype.systemId ? ' SYSTEM' : '') + (document.doctype.systemId ? ' "' + document.doctype.systemId + '"' : '') + '>'; for (; i < t.length; i += 1) l += ' ' + t[i].name + '="' + t[i].value + '"'; return d+'\n<html' + l + '>' + r + '</html>'; }
Now you can run this function:
console.log(document.fullHTML());
This will return HTML and doctype.
I ran this on example.com , here are the results
source share