I am trying to retrieve the full HTML of a page, so far I am using:
$('*').html()
Is there a better way (also this excludes the <html> tags at the top)?
I am trying to retrieve the full HTML of a page, so far I am using:
$('*').html()
Is there a better way (also this excludes the <html> tags at the top)?
I tried $('html').html() but this of course excludes the html element.
$('html').parent().html() didn't work either because the root has no parents.
$(document).html() also fails
You could get the contents and then capture any attributes from the html element that you require, at leas then you can rebuild it if needed
Edit based on comment: Just found this cross browser OuterHTML plugin http://yelotofu.com/2008/08/jquery-outerhtml/
function GetPageHtml()
{
return $('html')[0] + $('html').html() + '</html>';
}
Not sure what you're using this for, but would using $.get to fetch the location.href be a reasonable workaround (as a bonus you'd get the DOCTYPE and other header-ish stuff as well).
$.get(location.href, function(html) {
alert(html);
});
Ok maybe that's kinda silly...
This does the trick:
html = $('<dummy>').append($('html').clone()).remove().html();