Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7641175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:51:27+00:00 2026-05-31T08:51:27+00:00

I’m trying load some scripts dynamically with jQuery: var scripts = [‘script1.js’,’script2.js’,’script3.js’]; $.each(scripts ,

  • 0

I’m trying load some scripts dynamically with jQuery:

var scripts = ['script1.js','script2.js','script3.js'];

$.each(scripts , function(i, val) {
     $.getScript(val, function() {
     console.log('loaded '+ val);
});

But sometimes the order of loaded scripts changes. How can I load each script after the previous one has successfully loaded?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:51:29+00:00Added an answer on May 31, 2026 at 8:51 am

    You can load each after the previous has finished loading by using the callback function of $.getScript() as a recursive function call.

    //setup array of scripts and an index to keep track of where we are in the process
    var scripts = ['script1.js','script2.js','script3.js'],
        index   = 0;
    
    //setup a function that loads a single script
    function load_script() {
    
        //make sure the current index is still a part of the array
        if (index < scripts.length) {
    
            //get the script at the current index
            $.getScript(scripts[index], function () {
    
                //once the script is loaded, increase the index and attempt to load the next script
                console.log('Loaded: ' + scripts[index]);
                index++;
                load_script();
            });
        }
    }
    

    What’s occurring in your code is that the scripts are being requested at the same time and since they load asynchronously, they return and execute in random order.

    Update

    I haven’t tested this, but if the scripts are hosted locally, then you could try to retrieve them in plain text, then store all of the code in variables until they are all loaded at which time you could evaluate the scripts in order:

    var scripts   = ['script1.js','script2.js','script3.js'],
    
        //setup object to store results of AJAX requests
        responses = {};
    
    //create function that evaluates each response in order
    function eval_scripts() {
        for (var i = 0, len = scripts.length; i < len; i++) {
            eval(responses[scripts[i]]);
        }
    }
    
    $.each(scripts, function (index, value) {
        $.ajax({
            url      : scripts[index],
    
            //force the dataType to be `text` rather than `script`
            dataType : 'text',
            success  : function (textScript) {
    
                //add the response to the `responses` object
                responses[value] = textScript;
    
                //check if the `responses` object has the same length as the `scripts` array,
                //if so then evaluate the scripts
                if (responses.length === scripts.length) { eval_scripts(); }
            },
            error    : function (jqXHR, textStatus, errorThrown) { /*don't forget to handle errors*/ }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.