-
Running Javascript on ajax retrieved data
Hey all,
I'm fairly new to Ajax so I'd like to get some advice on a problem I'm facing. Basically, I have a site that I'm developing where I retrieve records from a database and display those records in a table on the main page. This is easy enough since I grab the necessary info via a php script and display it on the page using the innerHTML property.
The problem that I'm bumping into now is that I decided that I want to run some javascript on the returned data. Specifically, I want to use the Ajax Prototype library to apply an InPlaceEditor to each of the fields in the returned data. I have no way of knowing in advance how many records/fields I will be returning since the server will be doing the database lookup. The only way I can think of doing this is to have my Ajax request return something like the following:
<page>
<htmlcontent>
<div id='myid0'>blah blah</div>
<div id='myid2'>blah blah</div>
...
<div id='myid99'>blah blah</div>
</htmlcontent>
<js>
for(records=0;records<=99;records=records+1){
new Ajax.InPlaceEditor('myid'+records, 'billing.php');
}
</js>
</page>
So then when I retrieve this xml information, I parse out the htmlcontent and place it on the page using innerHTML and then parse out the javascript and run it using eval().
It seems to me that there must be a better way to run dynamic javascript than this. Anybody have any ideas?
-
well why not putting your JS code inside a function, and execute this function at the end of the retrieved data...
u can even put the JS coding directly after the html content:
<htmlcontent>
<div id='myid0'>blah blah</div>
<div id='myid2'>blah blah</div>
...
<div id='myid99'>blah blah</div>
<script type='text/javascript'>callToFunction();</script>
</htmlcontent>
this in case u really need to have xml, another way of doing that -quickly- is returning the data like that (non xml):
blabla1,blabla2,blabla3, ... ,blabla#
and in the main page strip out the "," from the returning Text (string) and then deal with it ur way ...
-
Similar Threads
-
Replies: 0
Last Post: 05-28-2007, 01:23 AM
-
Replies: 7
Last Post: 10-08-2006, 08:59 AM
-
By kamisetty in forum XML
Replies: 4
Last Post: 02-08-2002, 11:14 AM
-
By skrobism in forum VB Classic
Replies: 1
Last Post: 01-23-2001, 11:44 PM
-
By Jerry in forum authorevents.appleman
Replies: 2
Last Post: 04-13-2000, 03:33 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|