2

I would like to have javascript access the cilent's cache to check if they have an image loaded or not, can I do this or will that give me same origin errors?

Also what happens to websites that do not have

ccess-Control-Allow-Origin set to all?

user2303895
  • 21
  • 1
  • 2

2 Answers2

2

You can use this function (copied from this question) to test whether an image is in cache.

function cached(url) {
    var test = document.createElement("img");
    test.src = url;
    return test.complete || test.width+test.height > 0;
}

The fact is, once the browser has cached the image, there is no additional need for you to load the image from the cache, for the browser will do everything for you.

Thus, just insert a plain . Let the browser do its work.

Community
  • 1
  • 1
yzn-pku
  • 1,082
  • 7
  • 14
  • Seems you have resumed the original function for something shorter. If the var is set, you have loaded it. Seems the most direct way to do, but this is not a caché retrieval function. Just a load check/logging function. Does not have cache access the javascript? – m3nda Apr 18 '15 at 00:12
1

Not sure what you're trying to do but most browsers take care of that for you.

This means that before requesting the full image it usually sends a 'last modified date' of the cached image to the server, and the server will reply with a 304 unmodified status if the cached resource has not change.

For more information check this link:

http://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/

  1. Browser: Hey, give me logo.png, but only if it’s been modified since Mar 16, 2007.
  2. Server: (Checking the modification date)
  3. Server: Hey, you’re in luck! It was not modified since that date. You have the latest version.
  4. Browser: Great! I’ll show the user the cached version.
lostsource
  • 21,070
  • 8
  • 66
  • 88
  • Yes I do not have access to that server, can I load the external image on my server and see what is the cilent's response? – user2303895 Apr 21 '13 at 08:12
  • If it is on your own server (same-domain) you should be able to read headers of an XHR, check here http://stackoverflow.com/a/4881836/149636 – lostsource Apr 21 '13 at 08:22