Javascript autoloader to dynamically load javascript files (with require method)

An improved javascript autoloader.

This is an improvement on  thisl

 

 

Javascript autoloader to dynamically load other javascript files

A simple JavaScript autoloader:

(see improved version of this post here: http://webconfiguration.blogspot.co.uk/2013/11/javascript-javascript-autoloader-to.html)

var mydomain = {
    init: function(){
       
    },
    scripts_to_load: [‘http://code.jquery.com/jquery-1.10.2.min.js’,
                        ‘/common_js/global_function_rewrites.js’,
                        ‘/common_js/common-js.js’,
                        ‘/common_js/forum-js.js’,
                        ‘/js/mydomain.dummy.js’],
    load_scripts: function(){
        if( this.scripts_to_load.length > 0 ){
            var script = this.scripts_to_load.shift();
            this.script_loader( script, mydomain.load_scripts() );
        } else {
            this.init();
        }
    },
    script_loader: function(script, callback){
        callback = callback || false;
        var headID = document.getElementsByTagName(“head”)[0];        
        var newScript = document.createElement(‘script’);
        newScript.type = ‘text/javascript’;
        newScript.onload= function(){
            if(callback){
                callback();
            }
        };
        newScript.src = script;
        headID.appendChild(newScript);
    }
};
mydomain.load_scripts();

And in your global_function_rewrites.js one function to require bespoke js files for different pages:

function require( scriptLocation, callback ){
    callback = callback || false;
    mydomain.script_loader(scriptLocation,callback);
}