[英]javascript check if image is in browser cache
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 我得到以下结果false false,然后在firefox和IE上为true false(我假设它首先触发false false,然后在抓取图像后,它会触发true,false,但在chrome中它是false,false总是
any reason why? 有什么理由吗?
The answer on this question seems to work on Chrome. 关于这个问题的答案似乎适用于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. 回复/粘贴该问题,而不是我的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.