Last active
September 27, 2015 05:28
-
-
Save kgn/1218824 to your computer and use it in GitHub Desktop.
A javascript snippet that will preload all images defined in the styles sheets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment