Skip to content

Instantly share code, notes, and snippets.

@tedw
Created November 13, 2012 21:10
Show Gist options
  • Save tedw/4068422 to your computer and use it in GitHub Desktop.
Save tedw/4068422 to your computer and use it in GitHub Desktop.
Dynamically Insert a CSS File
/* Dynamically insert a CSS file */
/* Inspired by http://stackoverflow.com/questions/574944/how-to-load-up-css-files-using-javascript */
var insertCSS = function(cssId, path, width) {
var $ = document;
if( !$.getElementById(cssId) ) {
var head = $.getElementsByTagName('head')[0],
link = $.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = path;
link.media = ( width > 0 ? 'only screen and (min-width: ' + width + 'px)' : 'all' );
head.appendChild(link);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment