Skip to content

Instantly share code, notes, and snippets.

@okeydoke
Created January 14, 2016 19:42
Show Gist options
  • Save okeydoke/0b41702a5b24b68b7e66 to your computer and use it in GitHub Desktop.
Save okeydoke/0b41702a5b24b68b7e66 to your computer and use it in GitHub Desktop.
Async css loading
//http://stackoverflow.com/questions/5186638/how-to-asynchronously-load-css-using-jquery/32614409#32614409
var stylesheet = document.createElement('link');
stylesheet.href = '/inc/body/jquery/css/start/jquery-ui-1.8.10.custom.css';
stylesheet.rel = 'stylesheet';
stylesheet.type = 'text/css';
// temporarily set media to something inapplicable to ensure it'll fetch without blocking render
stylesheet.media = 'only x';
// set the media back when the stylesheet loads
stylesheet.onload = function() {stylesheet.media = 'all'}
document.getElementsByTagName('head')[0].appendChild(stylesheet);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment