Created
December 5, 2017 15:21
-
-
Save mgara/2706b6d10d018945f0b6b0dc2f410721 to your computer and use it in GitHub Desktop.
Load CSS dynamically with callback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As taken from a jsfiddle. (I'm not the owner)