Loading js files by yourself, supports multiple files, is incompatible with ie
The code copy is as follows:
/**
* Loading js file
* @param {string || array} url js path
* @param {Function} fn Callback after loading
* @return {object} game object
* @example
* getScript("url.js",fn)
* getScript(["url-1.js","url-2.js"],fn)
*/
game.getScript = (function() {
var cache = {};//The url is cached internally, and no request is required next time
return function(url, fn) {
if ("string" === typeof(url)) {
url = [url]; //If it is not an array, bring a set
};
var i = 0,//Change
OK = 0,//How many js are loaded successfully
len = url.length,//How many js are there in total
head = document.getElementsByTagName("head")[0],
js, _url,
create = function(url) {//Create js
js = document.createElement("script");
js.type = "text/javascript";
js.src = url;
head.appendChild(js);
return js;
};
for (; i < len;) {
if (cache[encodeURIComponent((_url = url[i++]))]) {//If loaded
(++ok >= len && fn) && fn();//If all js are loaded, execute the callback
continue;
}
cache[encodeURIComponent(_url)] = !0;//Set cache
js = create(_url);//Create js
fn && (js.onload = function() {
if (++ok >= len) {//If all js are loaded, execute the callback
fn();
}
});
};
head = js = _url = create = null;
return this;
}
})();