2013-04-20 110 views
0

我有下面的代碼,以檢查是否有外部圖像緩存或不,如果圖像是在瀏覽器緩存中的JavaScript檢查

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

我得到以下結果在Firefox和IE虛假,那麼對錯(這我想的是,它首先觸發虛假,那麼它抓住圖像後,它激發爲真,假,但在Chrome它是假的,總是

任何理由?

回答

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())); 

回答複製/粘貼的問題,而不是我的工作。

+0

請仔細閱讀我的文章 – user2280440 2013-04-20 07:25:37

+0

嗯,我在鉻(20我相信)測試了上述,它的工作。你確定你沒有圖像緩存嗎? – 2013-04-20 07:26:50

相關問題