Skip to content

Instantly share code, notes, and snippets.

@mgara
Created December 5, 2017 15:21
Show Gist options
  • Save mgara/2706b6d10d018945f0b6b0dc2f410721 to your computer and use it in GitHub Desktop.
Save mgara/2706b6d10d018945f0b6b0dc2f410721 to your computer and use it in GitHub Desktop.
Load CSS dynamically with callback
$.extend({
getCss: function(urls, callback, nocache){
if (typeof nocache=='undefined') nocache=false; // default don't refresh
$.when(
$.each(urls, function(i, url){
if (nocache) url += '?_ts=' + new Date().getTime(); // refresh?
$.ajax({
url: url,
cache: false,
success: function(){
$('<link>', {rel:'stylesheet', type:'text/css', 'href':url}).appendTo('head');
}
});
})
).then(function(){
if (typeof callback=='function') callback();
});
},
});
var cssfiles=['/css/normalize.css?jobofferinsidebar', '/css/tricks.css?jobofferinsidebar'];
$.getCss(cssfiles, function(){
console.log('all css loaded');
});
@mgara
Copy link
Author

mgara commented Dec 5, 2017

As taken from a jsfiddle. (I'm not the owner)

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