2

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
  • Are you injecting the script thru document.createElement('script')? Commented Jun 8, 2010 at 9:18

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

I want it to load after the search results via ajax have been loaded
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()[api.jquery.com/load/] method.
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
1

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

1 Comment

and how do i know when the content via ajax has loaded ?
0

jQuery has this functionality built in to the getScript function.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.