Skip to content

Instantly share code, notes, and snippets.

@ChaseWest
Created May 10, 2014 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChaseWest/861993bdb45573daa669 to your computer and use it in GitHub Desktop.
Save ChaseWest/861993bdb45573daa669 to your computer and use it in GitHub Desktop.
Parse HTML string to document object and insert into DOM
var HTML = "<div></span>Test</span></div>";
var body = document.querySelector("body");
function parseHTML(htmlString){
var parser = new DOMParser();
var fragment = document.createDocumentFragment();
fragment.appendChild(parser.parseFromString(htmlString, "text/html").querySelector("body"));
body.appendChild(fragment);
}
parseHTML(HTML);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment