Hi All,

Without getting into too much history on the why, I came into a situation where I needed to create a function to get various properties of !DOCTYPE so that other functions could adjust to work properly with certain DOCTYPEs (Strict, Transitional, etc.). I have tested the function in the following Windows browsers IE6+ FF3+ Chrome2+ and Safari3+ Opera9+. The only one that I can’t get working is Opera (testing with 9.64). I’m hoping that someone out that has a solution! Here is the code:

function getDOCTYPE(loc){
  loc=locEval(loc)||self; // locEval is a function i use to qualify window/frame paths
  var pid,sid,val,lng,ver,typ;

  try{ // this is the node model that should be supported by all browsers. IE gets a hard error with it though.
    pid=String(loc.document.doctype.publicId);
    sid=String(loc.document.doctype.systemId);
  }
  catch(err){
    val=String(loc.document.body.parentNode.parentNode.firstChild.nodeValue) // seems to only work in IE
    //val=document.getElementsByTagName("!")[0].nodeValue  // works in IE as well

    pid=val.replace(/^[^\"]*\"([^\"]+)\".*/,"$1");
    sid=((/http/).test(val))?val.replace(/^.*\"\s\"(http.*)/,"$1"):null
  }

  lng=((/xhtml/i).test(pid+sid))?"XHTML":"HTML";
  typ=((/strict/i).test(pid+sid))?"Strict": ((/trans|loose/i).test(pid+sid))?"Transitional": ((/frame/i).test(pid+sid))?"Frameset": ((/basic/i).test(pid+sid))?"Basic": ((/mobile/i).test(pid+sid))?"Mobile": null;
  ver=((/html\s*\d+\.?\d*/i).test(pid))?pid.replace(/^.*html\s*(\d+\.?\d*)\D*/i,"$1"):null;

  return {publicId:pid,systemId:sid,language:lng,type:typ,version:ver,dtd:lng+" "+typ+" "+ver}
}

//...

alert(getDOCTYPE().dtd)

Any help would be greatly appreciated! :)

To simplify the issue:
alert(document.body.parentNode.parentNode.firstChild.nodeValue) //does not seem to properly refrence the doctype node
// I have tried many other node paths and have had no luck accessing the doctype :(
alert(document.getElementsByTagName("!")[0].nodeValue) // I just stumbled across this trying different methods.

alert(document.doctype.publicId) //seems to be unsupported even though all references I have state that it is supported in XML and XHTML (for Opera)

By the way,
Chrome only works when using

<meta name="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
This article has been dead for over six months. Start a new discussion instead.