短版問題: 有沒有在所有瀏覽器,或替代工作navigator.mozIsLocallyAvailable同等功能?後負荷:檢查如果圖像是在瀏覽器緩存
龍版:)
嗨, 這裏是我的情況: 我要實現對asp.net MVC的的HtmlHelper擴展,輕鬆處理圖像後加載(使用jQuery)。
因此,我使用「alt」屬性中指定的源渲染帶有空圖像源的頁面。 我在「window.onload」事件之後插入圖像源,並且效果很好。
我做了這樣的事情:
$(window).bind('load', function() {
var plImages = $(".postLoad");
plImages.each(function() {
$(this).attr("src", $(this).attr("alt"));
});
});
的問題是:第一裝載後,後加載的圖像緩存。但是,如果頁面需要10秒鐘才能加載,則在此10秒後將顯示緩存的已加載後的圖像。
所以我認爲如果圖像被緩存以立即顯示它們,則在「document.ready」事件中指定圖像源。
我發現這個函數:navigator.mozIsLocallyAvailable來檢查圖像是否在緩存中。下面是我用jQuery做:
//specify cached image sources on dom ready
$(document).ready(function() {
var plImages = $(".postLoad");
plImages.each(function() {
var source = $(this).attr("alt")
var disponible = navigator.mozIsLocallyAvailable(source, true);
if (disponible)
$(this).attr("src", source);
});
});
//specify uncached image sources after page loading
$(window).bind('load', function() {
var plImages = $(".postLoad");
plImages.each(function() {
if ($(this).attr("src") == "")
$(this).attr("src", $(this).attr("alt"));
});
});
它適用於Mozilla的DOM,但它並不適用於其他任何一個。我嘗試了navigator.isLocallyAvailable:結果相同。
有沒有其他的選擇?
我認爲這將打破時緩存過期...您需要在localStorage中存儲響應頭文件(需要ajax調用)的過期日期,例如,在logCache中使用'localStorage [src] = expires'並在緩存中使用'now
Corin
2011-07-03 05:58:11
第二個想法可能不是一個大問題,因爲我們正在討論圖像...... – Corin 2011-07-03 05:58:57
如果手動清除緩存會發生什麼? – Parth 2016-03-20 08:40:58