2

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?

1 Answer 1

1

You need to send an Expires header with the image from the server to enable caching in the browser.

e.g. in Apache (in .htaccess)

enable expires:

ExpiresActive On

by date:

ExpiresDefault "access plus 30 days"

or by filetype:

ExpiresByType image/gif "modification plus 5 hours 3 minutes"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks scunliffe, I will be tweaking this, as I'm still new in the world of htaccess. But I've dabbled enough to know it's got advantages in many things.

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.