Preload Images with javascript

Posted by user271619 on Stack Overflow See other posts from Stack Overflow or by user271619
Published on 2010-04-12T23:24:36Z Indexed on 2010/04/12 23:33 UTC
Read the original article Hit count: 308

Filed under:

Here is my preload script:

<script type="text/javascript"> 
Image_1 = new Image(1,1);
Image_1.src = "images/sprites.png";
</script>

And it works fine. I can see in the headers the image loading.

Here's my question. Even though I have preloaded sprites.png, I notice that when I go to a page on my site that simply displays that entire image, the headers tell me my browser is requesting the server to send the image again.

I'm hoping to not sound too naive. But, why would the browser request the image when I preloaded it earlier? Are there other reasons the browser would need to re-request?

Also, I put the dimensions as Image(1,1). Sometimes I don't know the dimensions, as that image gets updated from time to time. Maybe that's a reason?

© Stack Overflow or respective owner

Related posts about image-preloader