0

so suppose i have a whole bunch of images in a page, and then I go to a different url that has the exact same images and layout, since the url is different the browser will reload all images,layout etc and will not display them from the cache even though they are the exact same images/layout....

is there a way to prevent this and control the way browser caches things using only javascript, php, or html?

EDIT/Addition

also what's interesting is that first of all I have two copies of the same site, one on my local server and another one on a remote hosting server.....

the one on the local server caches perfectly and none of the images reappear, whereas if I access the one on the remote server, the images would try to reappear....despite the fact that the files,etc are virtually identical between the local and remote server...

EDIT/Addition

Another interesting thing....the caching would work on IE and Chrome even for the remote server, but for some reason, it would screw up in firefox....

on the other hand the one in the local server would work even for firefox

kamikaze_pilot
  • 14,304
  • 35
  • 111
  • 171
  • Here's a good link to avoid image caching http://stackoverflow.com/questions/165253/javascript-how-to-force-image-not-to-use-browser-cache – Fran Verona Mar 17 '11 at 20:40
  • 1
    if the images are coming from the same place, unless your webserver is sending Cache-Control headers along with the images, they should definitely be loaded form the browser cache, regardless of the URL. Are you absolutely sure they're not being cached? How have you tested this? – glomad Mar 17 '11 at 20:40
  • Not sure what you mean by the "layout", but if you mean the HTML, then it will not be cached when you go to a different URL. There's not much you can do about that. – glomad Mar 17 '11 at 20:41

2 Answers2

1

Images if they have a fixed directory /images/image1.jpg will not be re-fetched unless you have set your headers to no-cache. It should not re download all the images that were on another page.

Test this with any variety of tools, for instance Firebug with Firefox or Safari / Chrome webkit web developer tracking. It will notify you if it was being cached or re-downloaded.

Hope that is what you are asking...

Jakub
  • 20,418
  • 8
  • 65
  • 92
  • firebug would display that some of the images' load bar are light grey (which I guess suggest that the image was loaded from cache)........the thing is...the images would still try to reappear (ie.) in the beginning there is nothing on the image placeholder and then the image begin to appear slowly and then display whereas in I've seen sites in which the image wouldn't even try to reappear...it's just there and the browser would just load the – kamikaze_pilot Mar 17 '11 at 20:53
  • different contents even when you go to a different url....so it feels like you haven't even left the page in the first place since all the images that are on the same spot are still there and woudln't even try to reappear.....is there a way to specifically accomplish this – kamikaze_pilot Mar 17 '11 at 20:54
  • does the url change to the file? (ie: relative link vs static link?) – Jakub Mar 17 '11 at 20:55
  • the images are all in the same directory....I'm using absolute references to them (ie. im including the domain) so the links are all the same...and since im using zend framework the urls would have different actions, etc and therefore different url structure – kamikaze_pilot Mar 17 '11 at 20:57
1

would asking the browser cache by using headers not help

    header("Cache-Control: private, max-age=5400, pre-check=5400");
    header("Pragma: private");
    header("Expires: " . gmdate("D, d M Y H:i:s"). " GMT");

there a good site to check returned headers http://redbot.org try it out it may help plus theres http://www.mnot.net/cache_docs/ you may want to look at if you dont figger your answer.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • note i changed header("Expires: " . gmdate("D, d M Y H:i:s"). " GMT"); as that redbot found an error with the original Expires: – Lawrence Cherone Mar 17 '11 at 21:29
  • yeah it's still trying to reappear – kamikaze_pilot Mar 17 '11 at 21:33
  • sometimes hosting company's dont give you the ability to set arbitrary HTTP headers. – Lawrence Cherone Mar 17 '11 at 21:38
  • so i checked out rebot and this appears: HTTP/1.1 200 OK Transfer-Encoding: chunked Date: Thu, 17 Mar 2011 21:38:42 GMT Server: LiteSpeed Connection: close X-Powered-By: PHP/5.2.14 Content-Type: text/html; charset=utf-8 Set-Cookie: PHPSESSID=0890f81336d24b802dea66af1f04cc89; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre- check=0 Pragma: no-cache Content-Encoding: gzip Vary: Accept-Encoding – kamikaze_pilot Mar 17 '11 at 21:40
  • which is weird since I specifically added header("Cache-Control: private, max-age=5400, pre-check=5400"); header("Pragma: private"); header("Expires: " . gmdate("D, d M Y H:i:s"). " GMT"); at my index.php on zend framework – kamikaze_pilot Mar 17 '11 at 21:41
  • sounds like your host is controlling the headers :/ – Lawrence Cherone Mar 17 '11 at 22:19