4

I was wondering if there was just simply a way to detect if a doctype is present - preferably an HTML5 doctype only. I don't want to return it in a string or anything (though I have already tried this with similar questions being asked here, see code below), rather, I just want to see if it doesn't exist. Like, if it is absent, call an alert, then return false, or if it is present, call an acceptance function.

Like I said previously, I have read other posts on here and other posts on other forums but none of them seem to match my answer, or maybe I'm interpreting the code incorrectly, if so please tell me.

The code I have already tried goes as follows:

    var msg = {
        noHTML5: "You're pages document type is either not defined or not compliant with HTML5."
    },

    node = document.doctype,
    thisNodeHTML = "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC"' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + ">";

    if(!thisNodeHTML) {
        alert(msg.noHTML5);

        return false;
    }

    else {
       // success = true;
    }

Edit:

I did notice, however, that once I took away the doctype in my html file, Chrome's error console catches it and displays an error. Maybe does my conditional statement have an incorrect syntax?

ModernDesigner
  • 7,539
  • 10
  • 34
  • 41
  • Possible duplicate: [Get DocType of an HTML as string with Javascript](http://stackoverflow.com/questions/6088972/get-doctype-of-an-html-as-string-with-javascript) – Brad Koch Dec 15 '12 at 23:35
  • The error in Chrome's console reads: `Uncaught TypeError: Cannot read property 'name' of null` right under line 29 (`thisNodeHTML = "<!...."`) – ModernDesigner Dec 15 '12 at 23:36
  • @BradKoch - that's exactly where I got my code I'm using now, but it returns it as a string instead of simply detecting it's presence. – ModernDesigner Dec 15 '12 at 23:37
  • Now, this code may be able to work, like I said in my `Edit`, such as the conditional statement being syntactically wrong. – ModernDesigner Dec 15 '12 at 23:38

1 Answers1

4

document.doctype is null when no doctype is present, thus your error. Tweak your method to return false in this condition, and you should be good. Sample detection function, based on the doctype:

var is_html5 = function () {
    if (document.doctype === null) return false;

    var node = document.doctype;
    var doctype_string = "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC"' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + ">";

    return doctype_string === '<!DOCTYPE html>';
};
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
  • Ha, ha! That's what I did. Used `if(thisNodeHTML == null || thisNodeHTML == "" || !thisNodeHTML) { ... }`. Thanks!! – ModernDesigner Dec 15 '12 at 23:46
  • The only problem is, if there is no doctype, I cannot display an `alert` because of the critical error, thereafter not parsing anymore code that has conjunction with `thisNodeHTML`. Is there anyway to bypass the error? – ModernDesigner Dec 15 '12 at 23:48
  • I pasted my test function in my answer for you (tested in Chrome v24). I'm guessing you're checking whether document.doctype === null too late? – Brad Koch Dec 15 '12 at 23:52
  • But ` ` is also a valid HTML5 doctype as per http://www.totalvalidator.com/support/doctypes.html – Dmitry Pashkevich Sep 03 '13 at 09:02