Skip to content

Instantly share code, notes, and snippets.

@antennaio
Created December 20, 2010 10:44
Show Gist options
  • Save antennaio/748243 to your computer and use it in GitHub Desktop.
Save antennaio/748243 to your computer and use it in GitHub Desktop.
Accessing Doctype with Javascript
var Doctype;
var publicId;
var search;
var htmlVersion;
if (document.doctype) {
publicId = document.doctype.publicId;
} else {
/* document.doctype doesn't exist - detect Explorer */
var IE = /*@cc_on!@*/false;
if (IE) {
if (document.all[0].nodeType == 8) {
Doctype = document.all[0].nodeValue;
search = Doctype.match(/^DOCTYPE\s+(html|math|svg)\s+(SYSTEM|PUBLIC)\s+"(.*\/\/EN)".*$/i);
}
if (search[3]) publicId = search[3];
}
}
if (publicId != null) {
if (publicId !== '') {
search = publicId.match(/^\-\/\/W3C\/\/DTD(.*)\/\/EN$/i);
htmlVersion = (search[1] != null) ? search[1] : 'Unknown';
} else {
htmlVersion = 'HTML 5';
}
} else {
htmlVersion = 'Unknown';
}
alert (htmlVersion);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment