Skip to content

Instantly share code, notes, and snippets.

@ksafranski
Created October 9, 2012 15:36
Show Gist options
  • Save ksafranski/3859578 to your computer and use it in GitHub Desktop.
Save ksafranski/3859578 to your computer and use it in GitHub Desktop.
Simple JavaScript Include Function
/*
* Simple JS Include Function
* Format: include({array_files},{callback});
* Example: include(['script1.js','script2.js'],function(){ alert('Loaded!'); });
*/
function include(array, callback) {
var loader = function (src, handler) {
var script = document.createElement("script");
script.src = src;
script.onload = script.onreadystatechange = function () {
script.onreadystatechange = script.onload = null;
handler();
};
var head = document.getElementsByTagName("head")[0];
(head || document.body).appendChild(script);
};
(function () {
if (array.length !== 0) {
loader(array.shift(), arguments.callee);
} else {
if (callback && typeof (callback) === 'function') {
callback();
}
}
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment