8

I know it's already discussed here, but there were no solution to get the whole document (including doctype).

$(document).html(); returns null...

dtrunk
  • 4,685
  • 17
  • 65
  • 109

8 Answers8

14

This will get you all the HTML:

document.documentElement.outerHTML

Unfortunately it does not return the doctype. But you can use document.doctype to get it and glue the two together.

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
  • 1
    You were the first mentioning `document.doctype`. So I accepted your answer. Thanks to all! – dtrunk Jan 31 '13 at 08:34
8

You can do

new XMLSerializer().serializeToString(document);

for all browsers newer than IE 9

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
4

try this.

$("html").html()

document is a variable it dose not represent the html tag.

EDIT

To get the doctype one could use

document.doctype
aefxx
  • 24,835
  • 6
  • 45
  • 55
Mortalus
  • 10,574
  • 11
  • 67
  • 117
4

This is a function which has support in IE6+, it does't use outerHTML for even more support, it adds the doctype and uses a few tricks to get the html tag and its attributes. In order to receive a string with the doctype, and doesn't use 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 the HTML and doctype.

I ran this on example.com, here are the results

Downgoat
  • 13,771
  • 5
  • 46
  • 69
1
document.documentElement.innerHTML 

will return you all document markup as string

to get the whole doctype read this

Community
  • 1
  • 1
Ph0en1x
  • 9,943
  • 8
  • 48
  • 97
1

I'm not sure about getting the complete doc.but what you can do is,you can get the content of html tag seprately and doctype seprately.

$('html').html() for content and document.doctype for getting the doctype

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

I don't think there is a direct access to the whole document (including the doctype), but this works :

$.get(document.location, function(html) {
    // use html (which is the complete source code, including the doctype)
});
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    i think it is more efficient to use the document already opened instead of "reloading" it by firing an ajax request... – dtrunk Jan 31 '13 at 08:30
  • 1
    @dtrunk yes but nothing else really gives the whole source code including the exact doctype typed (`document.doctype` is the nearest thing you have but not the exact one). – Denys Séguret Jan 31 '13 at 08:33
  • Additionally it can be noted that this usually won't fire a request as the document is cached. – Denys Séguret Jan 31 '13 at 08:43
0

I have done it on browser's console

document.documentElement;

Ashok Raj
  • 11
  • 2