Create a gist now

Instantly share code, notes, and snippets.

@kgn /preload.js
Last active Sep 27, 2015

A javascript snippet that will preload all images defined in the styles sheets.
var styles, matches, match, url, img = new Image(), loaded = [];
function forEach(a,c){if(a&&c){for(var i=0;i<a.length;++i){[i])}}};
forEach(this.styleSheets, function(){
forEach(this.rules, function(){
forEach(, function(){
if(this.toString() !== 'background-image')return;
forEach(matches, function(){
if(loaded.indexOf(url=match[1]) !== -1)return;
loaded.push(img.src = url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment