How do I return complete html of a page using jQuery?
I cannot do
return $('html').html()
or
return "<html>" + $("html").html() + "</html>";
because the page might not have html tag in it at all. I cannot use a class or id tag because this is not something that I control and can change. So the use case is if I pass a url I need to return complete html of the page and the page may or may not have html tagin it.
For what it’s worth,
$("html")will return an element in all sane situations, even if the source document didn’t contain an<html>element. This is because the browser will automatically insert certain required elements (including<html>,<head>, and<body>) into the DOM tree, even if they were not specified in the source document.The only exception I can think of (where you wouldn’t get anything from
$("html")) would be if you were running JQuery against a DOM tree that isn’t HTML — for instance, if you load JQuery into an SVG document. You’d be crazy to do that, though. 🙂