0

I have the following code to check if an external image is cached or not

<script type="text/javascript">
    function cached(url){
       var test = document.createElement("img");
       test.src = url;
       return test.complete || test.width+test.height > 0;
    }
    var base_url = "http://www.google.com/images/srpr/nav_logo80.png"
    alert("Expected: true or false\n" +
        cached(base_url)
        + "\n\nExpected: false (cache-busting enabled)\n" +
        cached(base_url + "?" + new Date().getTime()));         
</script>

I get the following result false false, then true false on firefox and IE (which I assume is that it first fires false false,then after it grabs the image, it fires true,false, but in chrome it is false, false always

any reason why?

kumar
  • 1,796
  • 2
  • 15
  • 37
user2280440
  • 1
  • 1
  • 2

1 Answers1

3

The answer on this question seems to work on Chrome.

function cached(url){
    var test = document.createElement("img");
    test.src = url;
    return test.complete || test.width+test.height > 0;
}
var base_url = "http://www.google.com/images/srpr/nav_logo80.png"
alert("Expected: true or false\n" +
  cached(base_url)
  + "\n\nExpected: false (cache-busting enabled)\n" +
  cached(base_url + "?" + new Date().getTime()));

Answer copy/pasted from that question, not my work.

Community
  • 1
  • 1
Glitch Desire
  • 14,632
  • 7
  • 43
  • 55