dcsimg


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
    Posts
    2

    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?

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    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 ...
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  3. #3
    Join Date
    Jun 2008
    Location
    Sydney Australa
    Posts
    7

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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