Skip to content

Instantly share code, notes, and snippets.

@egulhan
Created November 5, 2014 14:58
Show Gist options
  • Select an option

  • Save egulhan/e87c36374c61a30a1948 to your computer and use it in GitHub Desktop.

Select an option

Save egulhan/e87c36374c61a30a1948 to your computer and use it in GitHub Desktop.
How to load css by ajax
<script type="text/javascript">
var cssFiles=['css/custom/modal.css','css/custom/modal2.css'];
getCss(cssFiles,baseUrl);
</script>
/**
* Loads css file(s)
* @param string|array cssFiles
* @param string baseUrl (optional)
*/
function getCss(cssFiles,baseUrl)
{
if(!$.isArray(cssFiles))
cssFiles=[cssFiles];
for(var i in cssFiles)
{
var cur=baseUrl!==undefined ? baseUrl+'/'+cssFiles[i] : cssFiles[i];
$.ajax({
url:cur,
success:function(data){
$('head').append('<style type="text/css" media="screen">'+data+'</style>');
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment