Load JS scripts in order

by kosmos

JavaScript

var MyApp = {

    Scripts: {
        
        // put them with the correct order
        ToLoad: [
            '/path/to/script1.js',
            '/path/to/script2.js',
            '/path/to/script3.js'
        ],
        
        Loaded: 0
        
    },
    
    DoTheJob: function(){
        if( this.Scripts.ToLoad.length == this.Scripts.Loaded ) return;
        $.getScript(this.Scripts.ToLoad[this.Scripts.Loaded], function(){
            MyApp.Scripts.Loaded++;
            MyApp.DoTheJob();
        });
    }
    
};

// do it on ready (faster way)
$(function(){ 
    MyApp.DoTheJob();
});

// or do it on load
//window.onload = function(){ 
//    MyApp.DoTheJob();
//});