views:

323

answers:

2

Is the doctype part of the DOM, and if so, is there a good cross-browser way to read it? I'm not trying to do anything fancy. I just want to access the doctype information from within some JavaScript code. Read-only access is fine.

+4  A: 

document.doctype seems the (read-only) property you're looking for.

Alex Martelli
Specifically, the optional `DocumentType` node is a `childNode` of `Document`, just as the root `documentElement` and any `Comment` nodes outside the root are.
bobince
Thank you, Alex. bobince, the comment you left is also very informative.
@bobince Excellent comment.
Josh Stodola
Although, this does *not* work in IE.
Josh Stodola
Yeah, IE mis-parses the doctype as a comment. You can still find it through the `childNodes` list and read the content of the declaration as the comment's `data`, but you can't tell for sure that it wasn't actually a comment, and IE discards the leading and trailing two characters, assuming they're both going to be `--`. Ah, IE, bless it...
bobince
A: 

If you're inspecting the DOCTYPE to determine if you're in quirksmode or not, this is known to be cross-browser:

document.compatMode; // returns either "BackCompat" or "CSS1Compat"

So you can do:

var quirksmode = document.compatMode == "BackCompat";
darkporter