jQuery load CSS callback

To load a CSS file asynchronously with jQuery while providing a callback function for when the file has been included, use the following code:

var stylesheet = "foo.css";
var callback = function(){
  alert("CSS is now included");
};

$.get(stylesheet, function(contents){
  $("<style type=\"text/css\">" + contents + "</style>").appendTo(document.head);
  callback();
});

One Response to “jQuery load CSS callback”

  1. Some truly prime articles on this site, saved to fav.

Leave a Reply