Purpose
The ability to detect the DOCTYPE of webpages as well as attributes associated to it has been a challenge in the past. This new javascript function (which you call, most likely on page load) will report if the javascript is being loaded into a DOCTYPE defined webpage and will also report attributes associated to the DOCTYPE. This functions in Internet Explorer, Firefox, Safari, Chrome and Opera. To use, copy and paste the code below into your page and then call the function "captureDocTypeDefinition()"; the function will populate a couple of global variables that you can then reference as needed.
<script language="javascript" type="text/javascript">
/*
jsDocTypeDetect. Written by Joe McCormack, 2010. www.virtualsecrets.com.
*/
var captureDocTypeFound = 0; /* 1 = doctype definition found in page, 0 = not found */
var captureDoctypeLabel = ""; /* Output like: HTML */
var captureDoctypeType = ""; /* Output like: -//W3C//DTD XHTML 1.0 Transitional//EN */
var captureDoctypePath = ""; /* Output like: http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd */
function captureDocTypeDefinition() {
if (navigator.userAgent.toLowerCase().indexOf("msie") > 0) {
for (x = 0; x < document.getElementsByTagName("!").length; x++) {
if (parseInt(document.getElementsByTagName("!")[x].nodeType) == 8) {
if (document.getElementsByTagName("!")[x].nodeValue.toLowerCase().indexOf("doctype") > -1) {
tagData = new Array(); tagData = document.getElementsByTagName("!")[x].nodeValue.split("\"");
captureDoctypeLabel = tagData[0].replace("DOCTYPE ", "").toUpperCase(); captureDoctypeType = tagData[1]; captureDoctypePath = tagData[3]; captureDocTypeFound = 1;
}
}
}
}
else {
/* All Other Browsers */
for (x = 0; x < document.childNodes.length; x++) {
if (parseInt(document.childNodes[x].nodeType) == 10) {
captureDoctypeLabel = document.childNodes[x].name.toUpperCase();
captureDoctypeType = document.childNodes[x].publicId;
captureDoctypePath = document.childNodes[x].systemId;
captureDocTypeFound = 1;
}
}
}
}
</script>