We’re looking for ways to create a DOM document in javascript from a string, but without using Jquery.
Is there a way to do so? [I would assume so, since Jquery can do it!]
For those curious, we can’t use Jquery, becase we’re doing this in the context of a Chrome application’s content script, and using Jquery would just make our content script too heavy.
In case you’re still looking for an anwer, and for anyone else coming accross it, I just have been trying to do the same thing myself. It seems you want to be looking at javascript’s DOMImplementation:
http://reference.sitepoint.com/javascript/DOMImplementation
There are few references to compatibility as well here, but it’s fairly well supported.
In essence, to create a new document to manipulate, you want to create a new Doctype object (if you’re going to output some standards based stuff) and then create the new Document using the newly created Doctype variable.
There are multiple options to be put into both the doctype and the document, but if you’re creating an HTML5 document, it seems you want to leave most of them as blank strings.
Example (New HTML5 DOM Document):
The new Document now looks like this:
Example (New XHTML DOM Document):
So it’s up to you to populate the rest of it. You could do this as simply as changing
Or go with the more rigorous:
All yours.