DOM tree from AJAX response?

OK, this has probably been asked before, but it’s kind of hard to search for…

So, I want my javascript to pull in a web page, then extract a few divs from the HTML response. And then put those divs into the page.

But the HTML I get back from the ajax request will just be text html, right? Can I make a DOM out of it so that I can pick out the divs I want?

The HTML I’m pulling will probably not be well-formed. (I’m pulling it from multiple sites.)

thanks


var s = "<html><b></b><b></b><b></b>";
var div = document.createElement("div");
div.innerHTML = s;
alert(div.getElementsByTagName("b").length);

I’m not sure how reliable this is. Sounds dangerous though to just allow completely unfiltered content from another domain. Think about all the bad stuff it could contain, like scripts/media.

Yep I just found it too:

http://stackoverflow.com/questions/494143/how-do-i-create-a-new-dom-element-from-an-html-string-using-built-in-dom-methods

Actually, this is for a Greasemonkey script… hell, actually, I may just make it a .html file that people load locally from their hard drive, so not even greasemonkey.

thanks!