Skip to content

Instantly share code, notes, and snippets.

@louisje
Created July 13, 2012 04:33
Show Gist options
  • Save louisje/3102735 to your computer and use it in GitHub Desktop.
Save louisje/3102735 to your computer and use it in GitHub Desktop.
A RequireJS plugin to load css file
/**
* RequireJS Plugin - CSS Loader
*
* [ex.]
* define(['css!jquery-ui-css', 'jquery', 'jquery-ui'], { });
*
* Its better to put 'css!' in front of other js dependencies to get
* faster loading speed
*
*/
;(function() {
define({
load: function(name, req, load, config) {
var url = req.toUrl(name + '.css');
var css = document.createElement('link');
css.type = "text/css";
css.rel = "stylesheet";
css.href = url;
var heads = document.getElementsByTagName('head')
if (heads && heads.length > 0) {
heads[0].appendChild(css);
load(css);
} else {
load.error();
}
}
});
})();
@jpillora
Copy link

jpillora commented Sep 4, 2012

Has no load detection though, doing some testing with DOM events, will link you to my version if I get it working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment