skalee
skalee

Reputation: 12675

How to dump DOCTYPE element?

I wonder if there is a way to dump doctype to string.

For example, I have a web application which relies heavily on BackboneJS. Initial page source code is trivial (just requires scripts) but after loading Backbone generates whole content. I am seeking for a way to grab a source of DOM in particular state in order to send it to W3C validator. I don't want to just copy-paste contents from Firebug's inspector because I'd like to automatize process.

I've already found that I could dump HTML element with document.getElementsByTagName('html')[0].outerHTML. This is not standard, but works in many browsers (Chrome, Opera, maybe others). And this is OK, cause I am creating a tool for developers, not a solution for page end users users.

However, such a dump lacks info about doctype. And doctype is needed to instruct HTML validator. Could you help me, please? Thanks in advance for any help in this topic.

Upvotes: 0

Views: 93

Answers (2)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201588

The document.doctype object has the properties name (which is html for HTML documents), publicId for the public identifier, and systemId for the system identifier. You can thus construct the textual doctype declaration this way:

var doctype = '<' + '!DOCTYPE ' + document.doctype.name;
if(document.doctype.publicId) doctype += 
  ' PUBLIC "' + document.doctype.publicId + '"';
if(document.doctype.systemId) doctype += 
 ' "' + document.doctype.systemId + '"';
doctype += '>\n';

Upvotes: 2

tobspr
tobspr

Reputation: 8376

document.doctype

Hope that helps.

Upvotes: 1

Related Questions