Skip to content

Instantly share code, notes, and snippets.

@Fedia
Last active January 4, 2017 10:00
Show Gist options
  • Save Fedia/d685bb06fc5584e62136605cfa52bcfd to your computer and use it in GitHub Desktop.
Save Fedia/d685bb06fc5584e62136605cfa52bcfd to your computer and use it in GitHub Desktop.
Dynamic loading of external CSS and Javascript files. 287 bytes minified.
/**
* Dynamic loading of external CSS and Javascript files. 287 bytes minified. Usage:
* load(['some.css', 'lib.js'], function() { console.log('loaded!') });
*/
var load = function (urls, done) {
var doc = document;
var cnt = urls.length;
var head = doc.getElementsByTagName('head')[0];
var onload = function () {
if (--cnt === 0) done();
};
urls.forEach(function (url) {
var n;
if (url.substr(-4) === '.css') {
n = doc.createElement('link');
n.rel = 'stylesheet';
n.href = url;
} else {
n = doc.createElement('script');
n.src = url;
}
n.onload = onload;
head.appendChild(n);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment