Created
October 9, 2012 15:36
-
-
Save ksafranski/3859578 to your computer and use it in GitHub Desktop.
Simple JavaScript Include Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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