Create a DOM document from string, without JQuery

Julien Genestoux picture Julien Genestoux · Mar 7, 2012 · Viewed 24.5k times · Source

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.

Answer

apratimankur picture apratimankur · Jan 5, 2016

https://developer.mozilla.org/en-US/docs/Web/API/DOMParser

var parser = new DOMParser();
var doc = parser.parseFromString("<html_string>", "text/html");

(the resulting doc variable is a documentFragment Object).