Basically that's the question, how is one supposed to construct a Document object from a string of HTML dynamically in javascript?
There are two methods defined in specifications, createDocument
from DOM Core Level 2 and createHTMLDocument
from HTML5. The former creates an XML document (including XHTML), the latter creates a HTML document. Both reside, as functions, on the DOMImplementation
interface.
var impl = document.implementation,
xmlDoc = impl.createDocument(namespaceURI, qualifiedNameStr, documentType),
htmlDoc = impl.createHTMLDocument(title);
In reality, these methods are rather young and only implemented in recent browser releases. According to http://quirksmode.org and MDN, the following browsers support createHTMLDocument
:
Interestingly enough, you can (kind of) create a HTML document in older versions of Internet Explorer, using ActiveXObject
:
var htmlDoc = new ActiveXObject("htmlfile");
The resulting object will be a new document, which can be manipulated just like any other document.