DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Running Javascript on ajax retrieved data

  1. #1
    Join Date
    Sep 2006

    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:

    <div id='myid0'>blah blah</div>
    <div id='myid2'>blah blah</div>
    <div id='myid99'>blah blah</div>
    new Ajax.InPlaceEditor('myid'+records, 'billing.php');

    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?

  2. #2
    Join Date
    Oct 2005
    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:
    <div id='myid0'>blah blah</div>
    <div id='myid2'>blah blah</div>
    <div id='myid99'>blah blah</div>
    <script type='text/javascript'>callToFunction();</script>

    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 ...

  3. #3
    Join Date
    Jun 2008
    Sydney Australa

Similar Threads

  1. Data Service 2.0.0
    By zw_sea in forum Java
    Replies: 0
    Last Post: 05-28-2007, 01:23 AM
  2. Why I can't save data in data base?
    By Kurt in forum Java
    Replies: 7
    Last Post: 10-08-2006, 08:59 AM
  3. Replies: 4
    Last Post: 02-08-2002, 11:14 AM
  4. data and multiple forms
    By skrobism in forum VB Classic
    Replies: 1
    Last Post: 01-23-2001, 11:44 PM
  5. Passing data to a cgi written in javascript
    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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center