image preloader

by Ercan Cicek

JavaScript

// --- resources needed
// --- jQuery recommended
(function() { 
    var imgArr = ["imgSrc1", "imgSrc2", "imgSrcN"];
    preloadImages(imgArr);
})();

function preloadImages(imgArr) {
		// --- Version 1
    //$(imgArr).each(function() { $('<img/>')[0].src = this; });
    
    // --- Version 2 - better to append the images directly to the body (better for browser caching)
    $(imgArr).each(function(index) { 
    	$("body").append("<img src='" + this + "' style='visibility: hidden;' />");
    });
}