40

I know that I can access to doctype object via document.doctype or document.childNodes[0] but my problem is getting doctype as a string. I can do this in chrome and safari by calling document.doctype which returns <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">. However in Firefox, calling document.doctype returns DocumentType object.

Is there a way to get the doctype string in all browsers as in chrome and safari?

Thanks!

0

5 Answers 5

82

In all compliant browsers (including Chrome/Safari), document.doctype also returns a DocumentType object. The following code can be used to generate a valid DOCTYPE string.

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

This method returns the correct string for valid (HTML5) doctypes, eg:

  • <!DOCTYPE html>
  • <!DOCTYPE html SYSTEM "about:legacy-compat">
  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">

Explanation of the code:

node.name      # Holds the name of the root element, eg: HTML / html
node.publicId  # If this property is present, then it's a public document type.
               #>Prefix PUBLIC
!node.publicId && node.systemId
               # If there's no publicId, but a systemId, prefix SYSTEM
node.systemId  # Append this if present
Sign up to request clarification or add additional context in comments.

1 Comment

Worth noting documents with no DOCTYPE have a document.doctype === null.
58

You can also use this one liner to get the current doctype. This will work in any modern browser and IE 9 and higher.

new XMLSerializer().serializeToString(document.doctype);

1 Comment

This will be added xmlns attribute.
3
function get_doctype()
{
    var doctype = 
    '<!DOCTYPE ' + 
    document.doctype.name +
    (document.doctype.publicId?' PUBLIC "' +  document.doctype.publicId + '"':'') +
    (document.doctype.systemId?' "' + document.doctype.systemId + '"':'') + '>';
    return doctype;
}

Comments

1

Is that what are you looking for ?

alert(document.doctype.publicId);

2 Comments

That returns only -//W3C//DTD HTML 4.01//EN. I need whole DOCTYPE
Maybe you need to do --- var doc = '<!DOCTYPE '+document.doctype.name+' PUBLIC "'+document.doctype.publicId+'" "'+document.doctype.systemId+'">';
1

Concatenate DocumentType.name, .publicId and .systemId. Something like:

'<!DOCTYPE '+ 
  DocumentType.name+' PUBLIC "'+ //maybe you should check for publicId first
  DocumentType.publicId+'" "'+
  DocumentType.systemId+'">'

3 Comments

I'm guessing this doesn't support HTML5?
@Kendall: it does, but isn't very informative for html5 afaik.
I mean, it should print <!DOCTYPE html>, but for this page (which uses HTML5 doctype), I get <!DOCTYPE html PUBLIC "" "">.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.