Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read doctype with JavaScript

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.

like image 725
user241761 Avatar asked Dec 31 '09 23:12

user241761


People also ask

What is DOCTYPE in JavaScript?

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.

How do I open DOCTYPE in HTML?

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.

Does JavaScript need DOCTYPE?

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.

What is document URL in JavaScript?

URL. The URL read-only property of the Document interface returns the document location as a string.


2 Answers

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

like image 136
Alex Martelli Avatar answered Oct 25 '22 01:10

Alex Martelli


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";
like image 43
jpsimons Avatar answered Oct 25 '22 00:10

jpsimons