3
votes

We use $.getScript(url, callback) all over the place. In troubleshooting very occasional undefined errors, i noticed this from the jQuery documentation:

"The callback is fired once the script has been loaded but not necessarily executed."

Can anyone suggest a generic way to guarantee the callback is called after the script is executed?

I know I could poll for a var that gets defined in the script, but i'm hoping for a more elegant solution (because i'd have to make this change in hundreds of spots for hundreds of different variables)... like a callback I've missed from the documentation.

thanks in advance!

1

1 Answers

0
votes

You should be able to do this quite easily using getJSON.

http://api.jquery.com/jQuery.getJSON/

Something similar to this should do the trick:

$.getJSON( url , 
    function(data) {
       //Do something with data here 
       alert(data);
});

EDIT: The answer from the link below that I find most interesting is this one:

$.ajax({
    url: url,
    dataType: 'script',
    success: success,
    async: false
});

It seems that disabling the asynchronous part of ajax will execute the script first, then the callback afterwords, instead of doing them synchronously.