Created
March 23, 2017 11:29
-
-
Save evolutionxbox/63649d2a60ac6870f40d63a38f7f7d57 to your computer and use it in GitHub Desktop.
Load those styles all async like!
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
// 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