I made a simple script to load other scripts into the page without worrying about caching. It's meant to be used by a considerable amount of people, so I would like it to be as clean as possible, and I would like your advice.
window.load_scripts = function () {
var callbacks = {
jqueryCallback: function (script_path) {
$.getScript(script_path, function (data, textStatus, jqxhr) {
if (jqxhr.status === 200) {
console.log(script_path + ' loaded correctly.');
} else {
console.log(script_path + ' failed to load');
}
});
},
vanillaCallback: function (script_path) {
var script = document.createElement('script');
script.src = script_path + '?d=' + (new Date()).getTime();
document.head.appendChild(script);
}
}
var callback;
//if we don't have arguments, just return.
//no scripts have to be loaded
if (!arguments || !arguments.length)
return;
//if we have jquery, we will use $.getScript
callback = window.$ ? callbacks['jqueryCallback'] : callbacks['vanillaCallback'];
//loop through all scripts
for (let i = 0; i < arguments.length; i++) {
var path = arguments[i];
//just be sure that the argument is a string
if (typeof path === "string") {
callback(path);
}
}
}