(function($){ //cache needed for overagressive garbage collectors var cache = []; $.loadImages = function(images, callback) { // if our first argument is an string, we convert it to an array if (typeof images == "string") { images = [images]; } var imagesLength = images.length; var loadedCounter = 0; // Loop through our array for (var i = 0; i < imagesLength; i++) { // Create a DOM element for our image var cacheImage = document.createElement('img'); // Define onload event callback cacheImage.onload = function() { loadedCounter++; if (loadedCounter == imagesLength) { if (typeof callback == "function") { callback.call(); } } } // Add our image to the DOM cacheImage.src = images[i]; cache.push(cacheImage); } } })(jQuery)