How do I get the entire page's HTML with jQuery?

Viewed 92547

I used $(document).html(), but that threw an error... is there a way to get everything?

5 Answers

You could try:

$("html").html();

If you want to also capture the html tags you could concatenate them to the html like this:

function getPageHTML() {
  return "<html>" + $("html").html() + "</html>";
}

$("html").html() would get everything but the outer most html tags.

No need to lean on jQuery. The best and simplest approach is to use

new XMLSerializer().serializeToString(document)

which will always give you the contents of the entire page including DOCTYPE tag, and it is supported in all modern browsers: https://caniuse.com/#feat=xml-serializer

Related