Javascript provide us with the DOMParser class to parse XML/HTML into DOM.
But sometimes we will end up dealing with invalid html, the DOMParser do not provide a way to deal with syntax errors, it will simply stop and leave us hanging.
Another way would be to use document.implementation.createHTMLDocument
.
function getDocumentWithDomParser(html)
{
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/xml');
return doc;
}
// VS
function getDocumentWithCreateDocument(html)
{
var doc = document.implementation.createHTMLDocument('');
doc.documentElement.innerHTML = html;
return doc;
}