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
2009-12-31 23:46:44
Specifically, the optional `DocumentType` node is a `childNode` of `Document`, just as the root `documentElement` and any `Comment` nodes outside the root are.
bobince
2010-01-01 00:08:19
Thank you, Alex. bobince, the comment you left is also very informative.
2010-01-01 00:20:02
@bobince Excellent comment.
Josh Stodola
2010-01-01 00:45:22
Although, this does *not* work in IE.
Josh Stodola
2010-01-01 00:47:35
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
2010-01-10 00:40:18
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
2010-01-01 00:51:51