public
Last active

A javascript snippet that will preload all images defined in the styles sheets.

  • Download Gist
preload.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
(function(){
var styles, matches, match, url, img = new Image(), loaded = [];
function forEach(a,c){if(a&&c){for(var i=0;i<a.length;++i){c.call(a[i])}}};
forEach(this.styleSheets, function(){
forEach(this.rules, function(){
forEach(styles=this.style, function(){
if(this.toString() !== 'background-image')return;
if(!(matches=styles[this].match(/url\([^\)]+\)/g)))return;
forEach(matches, function(){
if(!(match=this.match(/url\(([^\)]+)\)/)))return;
if(loaded.indexOf(url=match[1]) !== -1)return;
loaded.push(img.src = url);
});
});
});
});
}).call(document)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.