Created
March 8, 2012 16:10
-
-
Save folletto/2001797 to your computer and use it in GitHub Desktop.
Dynamically parse a string to a DOM to be used with jQuery. It's very useful because it allows to parse non-XML content (either XML not declared as such or HTML)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_updateBody: function(str) { | |
/**************************************************************************************************** | |
* This function creates a new document object to be used to parse a HTML string. | |
* | |
*/ | |
var domDocument = document.implementation.createHTMLDocument(); | |
var range = domDocument.createRange(); | |
range.selectNode(domDocument.body); | |
var documentFragment = range.createContextualFragment(str); | |
domDocument.body.innerHTML = ""; | |
domDocument.body.appendChild(documentFragment); | |
return domDocument; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good stuff, Davide!
Gives me some motivation to dig through documentation :)