Skip to content

Instantly share code, notes, and snippets.

@andrewbrg
Last active November 27, 2018 22:30
Show Gist options
  • Save andrewbrg/78693dd7e390f45ef185888dec501fb4 to your computer and use it in GitHub Desktop.
Save andrewbrg/78693dd7e390f45ef185888dec501fb4 to your computer and use it in GitHub Desktop.
Load remote JavaScript files programatically at runtime
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Allows you to load JavaScript files programatically from within other JavaScript files in your project.
//
// Instructions:
// 1. Include this file before all other scripts in your project.
// 2. Call jsLoader.load() or jsLoader.loadAsync() from within your scripts passing a single JS file path or
// array of multiple JS paths to load into your script at runtime.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const jsLoader = {
_toArray: data => (Array.isArray(data) ? data : [data]),
load: files => {
files = this._toArray(files);
let script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', files[0]);
document.getElementsByTagName('head')[0].appendChild(script);
console.log("%cLoader.js loaded => " + "%c" + files[0], 'background: #222; color: #3BFF00', '');
if(files.length > 1){
this.load(files.slice(1));
}
},
loadAsync: files => {
let loadScript = (file) => {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if(xhr.readyState !== 4) {
return;
}
if(xhr.status === 200) {
try {
eval(xhr.response);
console.log("%cLoader.js xhr loaded => " + "%c" + file, 'background: #222; color: #3BFF00', '');
resolve(file);
return;
}
catch(e) {
console.log(
"%cLoader.js eval failed => " + "%c" + file + "%c. Attempting alternate method.",
'background: #222; color: #FFD400',
'',
''
);
}
}
let script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('async', 1);
script.setAttribute('src', file);
script.onload = script.onreadystatechange = (args, msg) => {
if(msg || !script.readyState || /loaded|complete/.test(script.readyState)) {
script.onload = script.onreadystatechange = null;
script = null;
if(!msg) {
console.log("%cLoader.js inject loaded => " + "%c" + file, 'background: #222; color: #3BFF00', '');
resolve(file);
}
else {
console.error("Loader.js failed => " + "%c" + file, 'background: #222; color: #FF2D00', '');
reject(file);
}
}
};
document.getElementsByTagName('head')[0].appendChild(script);
};
xhr.open('GET', file, true);
xhr.send('');
});
}
files = this._toArray(files);
return new Promise((resolve, reject) => {
let promises = files.map(x => loadScript(x));
return Promise.all(promises).then(files => resolve(files), files => reject(files));
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment