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 8324305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:01:08+00:00 2026-06-09T00:01:08+00:00

I am trying to dynamically include javascript files into my js file. I did

  • 0

I am trying to dynamically include javascript files into my js file. I did some research about it and find jQuery $.getScript() method would be a desired way to go.

// jQuery
$.getScript('/path/to/imported/script.js', function()
{
    // script is now loaded and executed.
    // put your dependent JS here.
    // what if the JS code is dependent on multiple JS files? 
});

But I am wondering whether this method can load multiple scripts at one time? Why I am asking this is because sometimes my javascript file is depending on more than one js files.

Thank you in advance.

  • 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-06-09T00:01:09+00:00Added an answer on June 9, 2026 at 12:01 am

    The answer is

    You can use promises with getScript() and wait until all the scripts are loaded, something like:

    $.when(
        $.getScript( "/mypath/myscript1.js" ),
        $.getScript( "/mypath/myscript2.js" ),
        $.getScript( "/mypath/myscript3.js" ),
        $.Deferred(function( deferred ){
            $( deferred.resolve );
        })
    ).done(function(){
        
        //place your code here, the scripts are all loaded
        
    });
    

    FIDDLE

    ANOTHER FIDDLE ​

    In the above code, adding a Deferred and resolving it inside $() is like placing any other function inside a jQuery call, like $(func), it’s the same as

    $(function() { func(); });
    

    i.e. it waits for the DOM to be ready, so in the above example $.when waits for all the scripts to be loaded and for the DOM to be ready because of the $.Deferred call which resolves in the DOM ready callback.


    For more generic use, a handy function

    A utility function that accepts any array of scripts could be created like this :

    $.getMultiScripts = function(arr, path) {
        var _arr = $.map(arr, function(scr) {
            return $.getScript( (path||"") + scr );
        });
            
        _arr.push($.Deferred(function( deferred ){
            $( deferred.resolve );
        }));
            
        return $.when.apply($, _arr);
    }
    

    which can be used like this

    var script_arr = [
        'myscript1.js', 
        'myscript2.js', 
        'myscript3.js'
    ];
    
    $.getMultiScripts(script_arr, '/mypath/').done(function() {
        // all scripts loaded
    });
    

    where the path will be prepended to all scripts, and is also optional, meaning that if the array contain the full URL’s one could also do this, and leave out the path all together

    $.getMultiScripts(script_arr).done(function() { ...
    

    Arguments, errors etc.

    As an aside, note that the done callback will contain a number of arguments matching the passed in scripts, each argument representing an array containing the response

    $.getMultiScripts(script_arr).done(function(response1, response2, response3) { ...
    

    where each array will contain something like [content_of_file_loaded, status, xhr_object].
    We generally don’t need to access those arguments as the scripts will be loaded automatically anyway, and most of the time the done callback is all we’re really after to know that all scripts have been loaded, I’m just adding it for completeness, and for the rare occasions when the actual text from the loaded file needs to be accessed, or when one needs access to each XHR object or something similar.

    Also, if any of the scripts fail to load, the fail handler will be called, and subsequent scripts will not be loaded

    $.getMultiScripts(script_arr).done(function() {
         // all done
    }).fail(function(error) {
         // one or more scripts failed to load
    }).always(function() {
         // always called, both on success and error
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to dynamically load a Javascript file using jQuery I keep getting a
I am trying to include Facebook's Javascript SDK (the all.js file from http://connect.facebook.com/en_US/all.js )
I am trying to dynamically load a jQuery plugin by calling getScript - but
What I'm trying to do is dynamically include one or more js files from
I am trying to dynamically request a file for download via a GET ajax
I'm trying to dynamically create alert messages using the jQuery plugin for Bootstrap CSS.
I am trying to use a textarea form entry which I have some javascript
I'm trying to dynamically generate an image file that will be composed by other
I'm trying to include an extension methods static class in a dynamically generated assembly,
I am trying to have some kind of dynamically growing array/data structure in C.

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.