Welcome to WebmasterWorld Guest from 54.163.35.238

Forum Moderators: open

Message Too Old, No Replies

parsing ajax responseText into JS DOM

can this be done, or do I need to parse the text itself?

     

tortuga

8:13 pm on Apr 30, 2007 (gmt 0)

10+ Year Member



Greetings,

This may be a very easy question, but for some reason I haven't been able to find the answer in the last hour or so of looking.

I'm a long-time web developer, but I'm brand new to Ajax. I'm working on a site that opens various forms in a floating Lightbox div. These forms are complete html pages, so non-js users just load it in their browser.

So, the Ajax request returns an html page. But, I need to modify that page/form based on which elements are present in the opening/parent page.

My question is, can I just pop the Ajax requestText into a standard JS DOM somehow, so I can use all the nifty methods for traversing the document tree, or do I have to parse the text directly? It seems like this should be easy to do, but I can't find the answer.

Thanks in advance.

Achernar

11:00 pm on Apr 30, 2007 (gmt 0)

5+ Year Member



I've done a little test (just being curious ;) ).

function getResponse(e) {
div=document.createElement('div');
div.innerHTML=e.responseText;
alert(div.firstChild);
// use this to list all first-level elements.
//for (var i=0,c;c=div.childNodes[i];i++) alert(c.tagName)
alert(div.innerHTML)
}

All the elements are immediate childs of the div element (but <head> and <body> disappear).
IE get rids of everything that is not inside <body> (<head>, <title>, <meta>). Mozilla keeps what was inside the <head> element.

cameraman

12:07 am on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



LOL potayto/potahto nifty/brain-stressing-walk-through-lava-field.

You'd probably want to use responseXML - it's an XML document reference.

Achernar

12:23 am on May 1, 2007 (gmt 0)

5+ Year Member



You'd probably want to use responseXML - it's an XML document reference.

Since the original poster mentionned that the request returns html

the Ajax request returns an html page

responseXML is not populated. ;)

cameraman

7:11 am on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



responseXML is not populated. ;)

Inneresting, I didn't realize that.
I did some experimenting, and if a text/xml content-type is returned, it does get populated (I don't know how fully - I just extracted the title and paragraphs). You could do a gateway script that serves the requested page with an xml header and manipulate it from there.

Achernar

9:30 am on May 1, 2007 (gmt 0)

5+ Year Member



I did some experimenting, and if a text/xml content-type is returned, it does get populated

The problem could then be with IE which is sometimes very picky on what it accepts.
A couple of days ago I had a problem with getElementsByTagName() that returned nothing in IE ; all this because one letter in the xml document was not encoded in the correct charset. ;)

tortuga

5:38 pm on May 1, 2007 (gmt 0)

10+ Year Member



Hey, thanks for the reponses. The first example should work fine for me, even if it is a bit of a workaround. I tried using the xml response, but as you said, it's not populated.

I'm trying to avoid doing anything different on the server side, i.e. changing the content/type. These pages are supposed to load in a Lightbox for JS users, and in a full browser window for non-JS users. So, keeping them plain xhtml seems simpler.

Thanks again for the tutelage.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month