3

I have this script that preloads a stack of images on my index page so that when people move on to my product page, the images are already in their cache. The problem is, when I remove the two alerts in the code so that it does not notify the user, the script won't work.

What am I doing wrong?

<script type="text/javascript">
  var imageFiles = new Array<?=$output?>
  var imagesLoaded = new Number(0);
  document.addEventListener("DOMContentLoaded", init, false);
  function init()
  {
    var images = new Array();

    alert("lkasd oe kdfl ke k");

    for(i=0; i<imageFiles.length; i++)
    images[i] = loadImage(imageFiles[i]);
  }
  function loadImage(url)
  {
    var image = new Image();
    image.addEventListener("load", imageLoaded, false);
    image.src = url;
    return image;
  }
  function imageLoaded()
  {
    imagesLoaded++;

    if (imagesLoaded == imageFiles.length)
      alert("Billederne er blevet preloadet og ligger i cachen.");
  }

</script>
3
  • how does it not work exactly? Commented Feb 8, 2012 at 16:45
  • 3
    I just can't believe that only 1 out of 11 answers was good enough so far. Commented Feb 8, 2012 at 16:48
  • 2
    Expanding on jAndy's comment: You should go back through your questions and mark the answers that solved your questions as Answered. People will be more likely to help you in the future if they see that you follow up. Commented Feb 8, 2012 at 16:49

1 Answer 1

9

I am guessing you didn't comment out your if as well as the alert.

We've had holy wars at my work about leaving off the { } for this very reason. =D

<script type="text/javascript">
  var imageFiles = new Array<?=$output?>
  var imagesLoaded = new Number(0);
  document.addEventListener("DOMContentLoaded", init, false);
  function init()
  {
    var images = new Array();

    //alert("lkasd oe kdfl ke k");

    for(i=0; i<imageFiles.length; i++)
    images[i] = loadImage(imageFiles[i]);
  }
  function loadImage(url)
  {
    var image = new Image();
    image.addEventListener("load", imageLoaded, false);
    image.src = url;
    return image;
  }
  function imageLoaded()
  {
    imagesLoaded++;

    //if (imagesLoaded == imageFiles.length)
    //  alert("Billederne er blevet preloadet og ligger i cachen.");
  }

</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.