When developing the latest version of Change content div(s) with JavaScript – faster page load time, I decided to craft this new jQuery plugin – getScripts:
You can load multiple external JavaScript files dynamically that can depend on each other!
The callback function is called only once…
Usage
$myScripts.getScripts(callback);
// The getScripts plugin
(function ($) {
// The Scripts class
var Scripts = function() {
this.a = function($this, cb) { var s = '';
if($this.length == 0) { cb && cb(); return; }
$this.each(function(i){
s += 'addsrc* = function() { $.getScript("*", *).fail(function(jqxhr, settings, exception) {alert(exception);}); }'.replace('*', i)
.replace('*', $(this).attr('src'))
.replace('*', i == $this.length - 1 ? 'cb' : 'addsrc' + (i + 1));
s += i == $this.length - 1 ? ';' : ',';
});
s = 'var ' + s + '\n' + 'addsrc0();';
try { eval(s); } catch(e) {alert(e);};
};
}; //end Scripts class
// Register jQuery function
$.fn.getScripts = function(cb) {
var $this = $(this);
$.fn.getScripts.o = $.fn.getScripts.o ? $.fn.getScripts.o : new Scripts();
$.fn.getScripts.o.a($this, cb);
return $this;
};




