Skip to content

Instantly share code, notes, and snippets.

@ijones922
Created July 8, 2013 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijones922/5952705 to your computer and use it in GitHub Desktop.
Save ijones922/5952705 to your computer and use it in GitHub Desktop.
Javascript: Preloading images
/**
* [ Preloading images is useful: Instead of loading an image when the user request it,
* we preload them in the background so they are ready to be displayed. Doing so in jQuery
* is very simple, as shown below:]
* Source: http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
* @param {[type]} $
* @return {[type]}
*/
(function($) {
var cache = [];
// Arguments are image paths relative to the current page.
$.preLoadImages = function() {
var args_len = arguments.length;
for (var i = args_len; i--;) {
var cacheImage = document.createElement('img');
cacheImage.src = arguments[i];
cache.push(cacheImage);
}
}
jQuery.preLoadImages("image1.gif", "/path/to/image2.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment