Skip to content

Instantly share code, notes, and snippets.

@mckamey
Created January 10, 2011 16:57
Show Gist options
  • Save mckamey/773044 to your computer and use it in GitHub Desktop.
Save mckamey/773044 to your computer and use it in GitHub Desktop.
DocType extraction snippet
function getDocType(document) {
var node = document.firstChild;
while (node) {
var nodeType = node.nodeType;
if (nodeType === 10) {
// doctype
var doctype = '<!DOCTYPE '+(document.documentElement.tagName || 'html').toLowerCase();
if (node.publicId) {
doctype += ' PUBLIC "' + node.publicId + '"';
}
if (node.systemId) {
doctype += ' "' + node.systemId + '"';
}
return doctype+'>';
}
if (nodeType === 8 && (""+node.nodeValue).toLowerCase().indexOf("doctype") === 0) {
// IE represents DocType as comment
return '<!' + node.nodeValue + '>';
}
node = node.nextSibling;
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment