Skip to content

Instantly share code, notes, and snippets.

@victorono
Created May 20, 2014 19:05
Show Gist options
  • Select an option

  • Save victorono/7ed938038d2026101032 to your computer and use it in GitHub Desktop.

Select an option

Save victorono/7ed938038d2026101032 to your computer and use it in GitHub Desktop.
jQuery.preloadImagesCSS = function() {
var allImgs = [],
k = 0,
sheets = document.styleSheets;
for (var i = 0; i<sheets .length; i++) {
var cssPile = '',
csshref = (sheets[i].href) ? sheets[i].href : 'window.location.href';
if (document.styleSheets[i].cssRules) {
var thisSheetRules = document.styleSheets[i].cssRules;
for (var j = 0; j<thisSheetRules.length; j++) {
cssPile+= thisSheetRules[j].cssText;
}
} else {
cssPile += document.styleSheets[i].cssText;
}
var imgUrls = cssPile.match(/[^\(]+\.(gif|jpg|jpeg|png)/g);
if (imgUrls !== null && imgUrls.length > 0 && imgUrls !== '') {
var images = jQuery.makeArray(imgUrls);
jQuery(images).each(function(index, image){
allImgs[k] = new Image();
allImgs[k].src = image;
k++;
});
}
}
return allImgs;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment