Skip to content

Instantly share code, notes, and snippets.

@aFarkas
Created April 22, 2011 10:18
Show Gist options
  • Save aFarkas/936413 to your computer and use it in GitHub Desktop.
Save aFarkas/936413 to your computer and use it in GitHub Desktop.
simple, small script loader
/*!
* SSSL: smallest, simpelst script loader
* version: 1.0.1
*
* API:
* Normal usage
* sssl(source [,complete]);
*
* Example:
* sssl('jquery.js', function(){
* $(function(){
* $('div').addClass('ready');
* });
* });
*
* -------------------------------
*
* Queued script loading (not so fast as yepnope/labJS, but ordered execution):
* sssl([source1, source2, source3], complete);
*
* Example:
* sssl(['jquery.js', 'jquery.ui.js'], function(){
* $(function(){
* $('div.accordion').accordion();
* });
* });
*/
(function(){
var firstScript = document.getElementsByTagName('script')[0];
var scriptHead = firstScript.parentNode;
var re = /ded|co/;
var onload = 'onload';
var onreadystatechange = 'onreadystatechange';
var readyState = 'readyState';
var load = function(src, fn){
var script = document.createElement('script');
script[onload] = script[onreadystatechange] = function(){
if(!this[readyState] || re.test(this[readyState])){
script[onload] = script[onreadystatechange] = null;
fn && fn(script);
script = null;
}
};
script.async = true;
script.src = src;
scriptHead.insertBefore(script, firstScript);
};
window.sssl = function(srces, fn){
if(typeof srces == 'string'){
load(srces, fn);
return;
}
var src = srces.shift();
load(src, function(){
if(srces.length){
window.sssl(srces, fn);
} else {
fn && fn();
}
});
};
})();
(function(){var d=document.getElementsByTagName("script")[0],f=d.parentNode,g=/ded|co/,e=function(b,c){var a=document.createElement("script");a.onload=a.onreadystatechange=function(){if(!this.readyState||g.test(this.readyState)){a.onload=a.onreadystatechange=null;c&&c(a);a=null}};a.async=true;a.src=b;f.insertBefore(a,d)};window.sssl=function(b,c){if(typeof b=="string")e(b,c);else{var a=b.shift();e(a,function(){if(b.length)window.sssl(b,c);else c&&c()})}}})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment