Skip to content

Instantly share code, notes, and snippets.

@evolutionxbox
Created March 23, 2017 11:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save evolutionxbox/63649d2a60ac6870f40d63a38f7f7d57 to your computer and use it in GitHub Desktop.
Load those styles all async like!
// TODO: convert into promise =)
// Load Style
function loadStyle(url, callback) {
if (!url || url === '') return;
var linkElement = document.createElement('link');
linkElement.href = url;
linkElement.setAttribute('rel', 'stylesheet');
if(typeof callback === 'function') {
linkElement.onload = callback;
}
document.head.insertAdjacentElement('beforeend', linkElement);
}
// Load Styles
/*
* Expects array of objects in the format:
* {
* url: 'path/to/style.css',
* callback: function () {}
* }
*
* The callback is optional.
*
*/
function loadStyles(styles, complete) {
if (styles.length > 0) {
styles.forEach(function(style) {
loadStyle(style.url, style.callback);
});
if (typeof complete === 'function') {
complete();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment