Preloading Images with jQuery and JavaScript

Function to preload images.

$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];

jQuery.preLoadImages("image1.gif", "/path/to/image2.png");


Share this snippet


Comments (2) - Leave yours

  1. Mark Simchock said:

    I like the Snippets Library idea. But as someone who’s learning (always) it would help if they were fully commented. Yes, to the fully experienced most things are obvious. But what about when you’re lower on the learning? Or just don’t have the wherewithal to dig in a sort something out.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please respect the following rules: No advertising, no spam, no keyword in name field. Thank you!