views:

343

answers:

2

My site loads images based on names that are created for them. It loads images assuming that they're there when sometimes they're not. Every time it loads an image like this:

<img src="/myimages/my-image.jpg" alt=""/>

if it's not there on the server, it seems to cache that there wasn't an image there, so next time I load up the same image after my-image.jpg has been saved to the server, it just shows up as blank... How can I make it so that it's not caching images that it doesn't find?

Edit: I still want to cache images that it finds

OR

Alternatively, how do you check that a file exists? (preferably from javascript). That way I could check if the image exists before loading it and I would completely subvert this problem.

Thanks,
Matt

A: 

perhaps insert a meta-tag into your code;

http://www.htmlgoodies.com/beyond/reference/article.php/3472881

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

and

<META HTTP-EQUIV="Expires" CONTENT="-1">

within the head section of the html..?

Phill
The thing is, if the image exists, I want it to be cached...
Matt
A: 

If you are able to, check from the server before the HTML file is ever sent to the client.

If this isn't an option, you can use ajax:

  1. call a page, passing in a request parameter of the file name
  2. In the page, check this file name and based on that pass back either true or false.
  3. Back in the javascript, if it's true (file exists), allow that image to load. Otherwise, load a default (placeholder) image, or just don't show any image at all.
Zack