How to parse HTML from JavaScript in Firefox?

By : hmp

What is the best way to parse (get a DOM tree of) a HTML result of XmlHttpRequest in Firefox?


I do not have the DOM tree, I want to acquire it.

XmlHttpRequest's "responseXML" works only when the result is actual XML, so I have only responseText to work with.

The innerHTML hack doesn't seem to work with a complete HTML document (in <html></html>). - turns out it works fine.

By : hmp


You can use the DOMParser to parse HTML - even tag soup:

var parser = new DOMParser()
parser.parseFromString('<!DOCTYPE html><html><head><title>hi</title></head><body><p>hello<b>world</b></p>', 'text/html')

I don't know if it handles partial table markup well, but it should create the same DOM the browser itself does for pretty much any markup.

By : hallvors

At least for newer Firefox versions, an easier way is or will soon be available. indicates that starting from FF11 it will be possible to ask for a DOM directly from the XHR by setting the responseType attribute to "document". At that point, the HTML will be parsed and the DOM stuck into responseXML as for an XML document.

innerHTML should work just fine, e.g.

// This would be after the Ajax request:
var myHTML = XHR.responseText;
var tempDiv = document.createElement('div');
tempDiv.innerHTML = myHTML.replace(/<script(.|\s)*?\/script>/g, '');

// tempDiv now has a DOM structure:
tempDiv.getElementsByTagName('a'); // etc. etc.
By : James

This video can help you solving your question :)
By: admin