Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery check doctype

Tags:

jquery

doctype

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

How to?

Thanks.

like image 658
Happy Avatar asked Oct 20 '25 01:10

Happy


2 Answers

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/

like image 68
Andy E Avatar answered Oct 22 '25 03:10

Andy E


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

like image 38
Fermin Avatar answered Oct 22 '25 05:10

Fermin