tags:

views:

53

answers:

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

How to?

Thanks.

+2  A: 

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

Fermin
Checking for real capability (instead of advertised capability) is indeed the only way to retain sanity.
Piskvor
+2  A: 

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/

Andy E
just for curiosity: try `$(document).siblings();` -> **ups**
jAndy
@jAndy: error for me in Chrome. `$(document.documentElement).siblings()` doesn't error, but it doesn't contain the doctype in the results.
Andy E
@Andy E's head: I receive an error aswell, that was the "ups" part. Either Mr. Resig forgot to check for that or he just assumed that this approach is just so useless no one would ever try :)
jAndy
@jAndy: I think it's probably a combination of both, it probably didn't occur to him.
Andy E
A: 

You could try:

var doc = $("DOCTYPE");

Digital Human