Skip to content

Instantly share code, notes, and snippets.

@ankane
Created February 18, 2011 18:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankane/834165 to your computer and use it in GitHub Desktop.
Save ankane/834165 to your computer and use it in GitHub Desktop.
/*
* Preload images that aren't visible on page load
* Works for images and background images
*
* $("#img1, #div2").preload()
*/
$.fn.preload = function() {
this.each( function() {
src = $(this).attr("src");
if (!src) {
bi = $(this).css("background-image");
var match;
if (match = bi.match(/url\(['"]?([^'"\)]+)['"]?\)/)) {
src = match[1];
}
}
if (src) {
$("<img/>")[0].src = src;
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment