How to check if the document marking is HTML5?

7

I'm creating a plugin and I need to know if the document is written in HTML5 or not.

I thought of using document.doctype , but I'm not sure what to compare it with since it compares with '<!DOCTYPE html>' false, because it's a node and not text.

How can I do this?

    
asked by anonymous 19.08.2015 / 22:26

2 answers

3

Have you ever tried to turn the object into a string?

var objSerializer = new XMLSerializer();
var doctypeName   = objSerializer.serializeToString(document.doctype);

console.log(doctypeName);

Then you can do the comparison.

if(doctypeName == "<!DOCTYPE html>"){
    console.log('É HTML 5');
}
    
19.08.2015 / 22:55
1

Considering that HTML5 documents are declared in the block at the beginning with: <!DOCTYPE html> , in addition there are the old versions as shown in this address:

link

If the HTML5 document does not usually get versioned, it means that if you see a systemId or a publicId declared in your block and that is different from: SYSTEM "about:legacy-compat" . And that this document contains a type of doctype defined by <!DOCTYPE html> or <!doctype html> , we can do the following verification:

 function checkHTML5() {
    var isHtml5 = document.doctype.nodeName;
    var systemId = document.doctype.systemId;
    var publicId = document.publicId;
    var xmlSerializer = new XMLSerializer();
    var doctypeHTML   = xmlSerializer.serializeToString(document.doctype);
    var doctypeName   = doctypeHTML.toLowerCase();
            if (isHtml5 == "html"                            &&
               (systemId == undefined                        ||
                systemId.indexOf("about:legacy-compat")!==-1 ||
                systemId == '')                              && 
                publicId == undefined                        ||
                publicId == ''                               &&
                doctypeName.indexOf("!doctype") !== -1) {
               return true;   
            }
    console.log(publicId);
    return false;
}
checkHTML5();
    
20.08.2015 / 20:44