views:

70

answers:

6

I am doing something like this: if an image is cached on user's computer and its timestamp is the same as the one on the server, then display the cached version; otherwise, do NOT load the image from the server.

I guess maybe JavaScript can do this, so I tagged this post as javascript. If it is improper, please help me to re-tag it.

Edit: Here I give more details about what I am going to implement. I am working on something like a web-based file explorer, where thumbnails are loaded only if the user click on a "view thumbnail" button beside each image. If a thumbnail is already cached, use the cached version; otherwise, show a generic image icon.

A: 

This isn't something you have to worry about. The browser does this automatically for you. You do have to make sure that the server is sending the right headers. It does help to have proper cache settings on the files to avoid the additional HEAD request.

Bruce Armstrong
@Bruce - I think normally if the file is not in the browser cache, it will load from the server. That is not what I want. I want to prevent downloading.
Ethan
I need to work on my reading skills, sorry about that.
Bruce Armstrong
You might be able to get a similar result with cookies. If you pass a unique cookie with each image, then there would be something concrete to check.
Bruce Armstrong
@Bruce - Thx. But cookies may be not a good way for my project since the number of images can be big.
Ethan
A: 

Could you approach this from a different direction and use cookies and PHP instead?

Trevor Boyle
... it would appear that Bruce has the same idea.
Trevor Boyle
A: 

Couldn't you just move/delete/rename it on the server? And then handle the broken image with a javascript Image.onError to hide the broken image tag for the people that don't have the cached image.

I think that would show cached version for those whohave it in their broswer chache. The rest of the people will not se anything

Charlie boy
@Charlie - Thanks for your idea but this is not the way to go with my project. Just edited the post and gave more details about what I am going to implement.
Ethan
A: 

A browser can send the "If-Modified-Since" and "ETag" headers to indicate to the server that it does have a version of a resource. Check the W3 caching rules regarding these headers.

You could query for those headers on the server, sending back the generic icon if not found, and a "304 Not Modified" response if found.

Neil Moss
+1  A: 

This seems a bit odd to me.

You describe your functionality as showing a list of files and giving the user the option to click "view thumbnail". This means it will only attempt to load the image from the server if the user specifically requests the image - so you have already stopped the potential for a mass-load of a large number of images.

So if the image isn't in my browser cache, which on my first visit none will be, then I will get the generic image icon. On my next visit, the image still won't be in my cache so I will get the generic image icon again. So how will I ever see anything except the generic image icon?

Now onto a solution, which I admit doesn't meet your requirements, but this is a typical implementation for this type of software.

  1. Show the files with a "show thumbnail" option
  2. When the "show thumbnail" option is selected, load the thumbnail from the server (note that quite often a script will serve a resized image, which may also be stored on the server to avoid having to process the resize again for a certain amount of time)
  3. The browser will automatically cache the image and prevent further server requests for a time
Sohnee
@Sohnee - You got the whole point! I just want to make it smarter that, if a thumbnail is in the cache, the "view thumbnail" button won't show at all (removed with javascript)
Ethan
@Ethan - As you explain, if the image is cached it will show. If it is not cached it will show a generic image. Sounds like you could write some logic that says if(generic_image is visible) hide_view_thumbnail... right?
Flash84x
@Flash84x - I would like to write some logic that says `if (image_thumbnail is in cache) { generic_image.replaceWith(image_thumbnail); view_thumbnail_button.hide(); }`. Therefore I am wondering if javascript can determine if a file is cached.
Ethan
+1  A: 

My RSS reader, for feeds that have a lot of posts, loads a full page of posts. The number of entries is dependent on the size of the actual window. When I scroll to the bottom of the list, if there are more posts it will load another page.

Would that be applicable to your file explorer? It would mean that a user would always load twenty thumbnails but never any more unless it's necessary. I've never used a file explorer that required me to click to view a thumbnail.

mqsoh
@mqsoh - Sounds like a good solution! I'll decide on the way to go tomorrow.
Ethan