include multiple js files using jQuery $.getScript() 1

include multiple js files using jQuery $.getScript()

$.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
    
});

Here is what the above code is Doing:
1. The $.getScript() method loads the scripts asynchronously, but in the order specified.
2. The $.Deferred() method is used to create a deferred object that is resolved when the document is ready.
3. The $.when() method is used to execute a function when all the deferred objects are resolved.
4. The done() method is used to execute a function when the deferred object is resolved.

Similar Posts