Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  18] [ 6]  / answers: 1 / hits: 20530  / 15 Years ago, wed, may 20, 2009, 12:00:00

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



EDIT:



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.


More From » ajax

 Answers
90

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.childNodes;
tempDiv.getElementsByTagName('a'); // etc. etc.

[#99494] Saturday, May 16, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brodyfrancisi

Total Points: 1
Total Questions: 102
Total Answers: 89

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
brodyfrancisi questions
;