Skip to content

Instantly share code, notes, and snippets.

@ckimrie
Last active December 17, 2015 09:29
Show Gist options
  • Select an option

  • Save ckimrie/5587884 to your computer and use it in GitHub Desktop.

Select an option

Save ckimrie/5587884 to your computer and use it in GitHub Desktop.
Lightweight CSS loader
/**
* Lightweight CSS Loader
*
* Even prepends the correct theme URL location to allow easy script loading
*
* @param {string} name URL to CSS file
* @param {Function} cb Callback function
* @return {null}
*/
function loadCss(name, cb) {
var def = new jQuery.Deferred(),
url = String(name),
eed = document.createElement('link');
eed.type = 'text/css';
eed.rel = 'stylesheet';
eed.href = url;
//Listen for script load
eed.onload = eed.onreadystatechange = function () {
if (typeof cb === "function") {
cb();
}
};
document.getElementsByTagName('head')[0].appendChild(eed);
}
/**
* Usage
*/
loadCss('http://path/to/file.css', function(){
//Code to run once CSS is loaded
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment