tags:

views:

111

answers:

3

I want to use a function on my html page but the content is delivered via ajax, how do I first load the script and then apply the function ? It won't work if I include the script in my <head> . Also how can I use the jQuery .find() and and apply a function or modify the css for content that has been delivered after the page has loaded ?

+1  A: 

See http://api.jquery.com/load-event/

mathk
and how do i know when the content via ajax has loaded ?
andrei
+1  A: 

LABjs(http://labjs.com/) has an awesome interface for that. Here's an example code.

<!-- In the head -->
<script src="lab.js"></script>
<!-- In the body -->
<script>
  $LAB
    .script("yourscript.js")
    .wait(function(){
          yourfunction();
     });
</script>

In the above example, the yourfunction(); will only be called after the script yourscript.js has been loaded.

Edit: After an ajax call using the .load method, this is how your body script might look like.

<script>
  $('taggetelmselector').load(your_url, data, function(){
      $LAB
      .script("yourscript.js")
      .wait(function(){
            yourfunction();
       });
  });
</script>
partoa
I want it to load after the search results via ajax have been loaded
andrei
While you will still need to use LABjs, you need to add an ajax complete event handler to your ajax call. I'll update the example to assuming you're working with the .load()[http://api.jquery.com/load/] method.
partoa
Found the answer:i used $.get(); combined with $.getScript for an auto-refresh of the scripts this way they kept account of the new elements
andrei
Well, that too.
partoa
Thanks a lot ! :)
andrei
A: 

jQuery has this functionality built in to the getScript function.

    $.getScript('yourscript.js', yourfunction);
Jeff Davis