homepage Welcome to WebmasterWorld Guest from 184.73.52.98
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
parsing ajax responseText into JS DOM
can this be done, or do I need to parse the text itself?
tortuga




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

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




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

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




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

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

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

Achernar




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

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




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

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




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

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




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved