admin管理员组

文章数量:1305141

I am using jQuery's $.ajax() to get some data. I'd like to include that data in a popover dialog. I'm using the Twitter Bootstrap popovers.

It's not working; I believe the problem is that the JS for the popovers gets loaded before the data arrives.

How do I do something like:

<script src="{{ STATIC_URL }}js/bootstrap-popover.js"></script>

inside of my $.ajax() success function?

var request = $.ajax({
    url: requestUrl,
    dataType: "jsonp",
    success: function(data) {
       ...
    }

I am using jQuery's $.ajax() to get some data. I'd like to include that data in a popover dialog. I'm using the Twitter Bootstrap popovers.

It's not working; I believe the problem is that the JS for the popovers gets loaded before the data arrives.

How do I do something like:

<script src="{{ STATIC_URL }}js/bootstrap-popover.js"></script>

inside of my $.ajax() success function?

var request = $.ajax({
    url: requestUrl,
    dataType: "jsonp",
    success: function(data) {
       ...
    }
Share Improve this question asked Jan 10, 2012 at 21:42 coffee-grindercoffee-grinder 27.6k19 gold badges59 silver badges82 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

you can use jQuery's $.getScript method:

success: function(data) {
  $.getScript("myurl/js/bootstrap-popover.js");
}

In trying to offer you a better solution, your tentative conclusion doesn't make sense to me. You should be able to include the popover javascript long before your ajax call and then use code to actually invoke the popover or configure it on any newly added content in your success handler.

There is no reason I'm aware of to load the popover js inside the success handler. Load it beforehand (in the normal way you load your other scripts) and then use it in the success handler for your ajax call. To help with how you'd use the popover script, we'd need to know more about what you're trying to do with it.

本文标签: How do you load a Javascript file inside of the success function for ajax()Stack Overflow