Happy
Happy

Reputation: 891

jQuery check doctype

If doctype is <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    // do something
else 
    // do something

How to?

Thanks.

Upvotes: 4

Views: 4522

Answers (5)

zrajm
zrajm

Reputation: 1392

document.doctype and document.firstChild both seem to return the doctype, though I do not know how widely supported they are.

Upvotes: 2

Paulo Guedes
Paulo Guedes

Reputation: 19

Try using the attribute "this.document.doctype" If the isn't declared, the result will be null, otherwise, the result will be the object with the doctype.

Upvotes: 1

Andy E
Andy E

Reputation: 344517

Right, I'm back after testing this in IE, Chrome, Firefox and Opera. IE will give you the full doctype with the following piece of code:

var doctype = document.documentElement.previousSibling.nodeValue;
// -> DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

Unfortunately, this is probably incorrect, as Chrome, Firefox and Opera return null for nodeValue. Since none of them support outerHTML, I can't think of a way to get the full doctype, but you can get individual parts:

 var doctype = document.documentElement.previousSibling;

 console.log(doctype.systemId)
 // -> http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd

 console.log(doctype.publicId)
 // -> -//W3C//DTD XHTML 1.0 Strict//EN

However, that doesn't work in IE, but it wouldn't be too difficult to parse those out. You can use an if statement to check that nodeValue is not null and fall back to checking systemId or publicId.

Script used to run the tests: http://jsfiddle.net/Cwb8q/

Upvotes: 3

Digital Human
Digital Human

Reputation: 1637

You could try:

var doc = $("DOCTYPE");

Upvotes: -5

Fermin
Fermin

Reputation: 36071

You could use the jQuery.support object to check for specific browser features (e.g. BoxModel) and working against them.

Upvotes: 2

Related Questions