8

I'm curious if there is a way to create DocType nodes via W3C DOM? The spec explicitly states that they are readonly and cannot be edited, but are they able to be created?

http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-412266927

The Document interface doesn't seem to have any create method for it:

http://www.w3.org/TR/DOM-Level-3-Core/core.html#i-Document

Perhaps the broader question is: Can an entirely new HTML Document be built programmatically via DOM? I know about document.createDocumentFragment() but I mean the equivalent of a root HTML document with specific doctype, etc.


Update: it looks like recent Gecko and WebKit implement DOMImplementation.createDocument and DOMImplementation.createDocumentType but not IE8 or before (haven't checked IE9). So unfortunately I'm still stuck there.

I am also a bit unsure what I can do with this new Document instance once I have it. All of the current DOM methods hang off the global document object, so there doesn't seem to be a way to swap it with a new one.

mckamey
  • 17,359
  • 16
  • 83
  • 116

1 Answers1

1

You need a DOMImplementation object. See the section Bootstrapping.

Alohci
  • 78,296
  • 16
  • 112
  • 156
  • I'm not seeing how that answers my question. The `DOMImplementation` object appears to allow you to query the supported DOM modules. Can you be more specific about how one could use this to create new instances of these nodes? – mckamey Nov 18 '10 at 20:44
  • DOMImplementation (http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-102161490) has methods createDocument and createDocumentType. – Alohci Nov 18 '10 at 20:59
  • Ah okay, so it looks like you can get one of those objects from `document.implementation`. Can you replace the current document with one created this way? – mckamey Nov 18 '10 at 21:51
  • Although I've seen web pages use document.implementation, I've never used it myself, so sorry I don't know. Do you want to replace the current document permanently or swap backwards and forwards between 2 or more documents? You might be better off starting a new question. – Alohci Nov 19 '10 at 00:36
  • Thanks. This is the new question about replacing the current document: http://stackoverflow.com/questions/4297877 – mckamey Nov 28 '10 at 17:13