Last active
January 4, 2017 10:00
-
-
Save Fedia/d685bb06fc5584e62136605cfa52bcfd to your computer and use it in GitHub Desktop.
Dynamic loading of external CSS and Javascript files. 287 bytes minified.
This file contains hidden or 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
/** | |
* 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