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.
The doctype property returns a document's doctype (as a DocumentType object). The doctype property returns null if the document has no doctype. The doctype property is read-only. The doctype.name property returns the name of the doctype.
A doctype declaration tells the browser that the page to be rendered is written in HTML. To declare an HTML5 doctype, `<! DOCTYPE html>` is required in the first line of your HTML document. Doctype declaration for HTML5 is not case sensitive and does not require a closing tag.
DOCTYPE declarations are only required for html based documents. Other JavaScript or CSS files are actually loaded into the page with <link> or tags. These tags signify the file type to the browser thereby allowing the browser to properly parse the file.
URL. The URL read-only property of the Document interface returns the document location as a string.
document.doctype
seems to be the (read-only) property you're looking for.
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With