Skip to content

Instantly share code, notes, and snippets.

@matijs matijs/loadCSS.js
Created Feb 23, 2015

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

matijs commented Feb 25, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.