How to get the entire document HTML as a string?

Robin Rodricks picture Robin Rodricks · May 3, 2009 · Viewed 359.3k times · Source

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

document.documentElement.??

Answer

Colin Burnett picture Colin Burnett · May 3, 2009

MS added the outerHTML and innerHTML properties some time ago.

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.

See quirksmode for browser compatibility for what will work for you. All support innerHTML.

var markup = document.documentElement.innerHTML;
alert(markup);