2

I am trying to load multiple .js files through the GetScript method. I need them to be loaded in series, meaning the first .js files needs to be fully loaded before going to the next one.

Question 1: I was using the code below, but found that this becomes quite messy when the amount of js files increase. Is there a more clear way of loading multiple .js files?

Also, I've found that function( data, textStatus, jqxhr ) is a callback function that is executed when the getScript request succeeds. The parameters (data, textStatus, jqxhr) can be used in the funtion, where: data is the data from the .js file, textStatus is the status of the request and jqxhr is a XMLHttpRequest object.

Question 2: Why is the parameter textStatus used as a callback? Using it in this function will always set it to Success, right?

Question 3: When I only want to execute the next getScript, i could just leave out the parameters and write: jQuery.getScript("url.min.js", function() { Correct?

jQuery.getScript("//url1.com/file1.js", function(data, textStatus, jqxhr) {
  jQuery.getScript("//url2.com/file2.js", function(data, textStatus, jqxhr) {
    jQuery.getScript("//url3.com/file3.js", function(data, textStatus, jqxhr) {
      jQuery.getScript("//url4.com/file4.js", function(data, textStatus, jqxhr) {
      });
    });
  });
});

Thank you.

0

1 Answer 1

3

I was using the code below, but found that this becomes quite messy when the amount of js files increase. Is there a more clear way of loading multiple .js files?

You could DRY it up a little by recursively iterating through an array of URLs, assuming that no further processing is required in the callback. Something like this:

function getScript(arr, i) {
  i = i || 0;
  jQuery.getScript(arr[i], function() {
    i++;
    arr.length > i && getScript(arr, i);
  });
}
getScript(['//url1.com/file1.js', '//url2.com/file2.js', '//url3.com/file3.js', '//url4.com/file4.js']);

Why is the parameter textStatus used as a callback? Using it in this function will always set it to Success, right?

It follows the same pattern as other jQuery AJAX methods, you're right that in this case it will always be a successful response.

When I only want to execute the next getScript, i could just leave out the parameters and write: jQuery.getScript("url.min.js", function() { Correct?

Yes

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks. I do get a js (console) error: Error in script declaration; Error:scriptUrls is not defined
Apologies, that should be arr[i]. I've updated the answer for you
Works, thanks. To be sure; this loads the next .js file, after the previous one is loaded succesfully correct?
That's right. The logic flow is the same as your original, just less repetetive :)

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.