javascript检查图像是否在浏览器缓存中

时间:2013-04-20 07:21:43

标签: javascript google-chrome

我有以下代码来检查外部图像是否被缓存

<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>

我得到以下结果为false false,然后在firefox和IE上为true false(我假设它首先触发false false,然后在抓取图像后,它会触发true,false,但在chrome中它是false,总是假的

任何原因?

1 个答案:

答案 0 :(得分:2)

this question上的答案似乎适用于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()));

回答/粘贴该问题,而不是我的工作。