Skip to content

Instantly share code, notes, and snippets.

@matijs
Created February 23, 2015 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matijs/17638e91c9ce1cee3e04 to your computer and use it in GitHub Desktop.
Save matijs/17638e91c9ce1cee3e04 to your computer and use it in GitHub Desktop.
load CSS using JS
function loadCSS(href, options){
'use strict';
options = options || {};
var styleSheet = document.createElement('link');
var ref = options.before || document.getElementsByTagName('script')[0];
styleSheet.media = 'not all';
styleSheet.rel = 'stylesheet';
styleSheet.href = href;
styleSheet.onload = function () {
styleSheet.onload = null; // only run once
styleSheet.media = options.media || 'all';
if (typeof(options.callback) === 'function') {
options.callback();
}
};
ref.parentNode.insertBefore(styleSheet, ref);
return styleSheet;
}
@matijs
Copy link
Author

matijs commented Feb 25, 2015

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