我有一些AJAX設置間隔來檢查服務器的通知等問題是瀏覽器沒有緩存像我想象的那樣的圖像。使用Javascript緩存圖片 - ajax重新加載它們
每隔2.5秒運行一次間隔,並設置來自XML的div的innerHTML,包括圖像。
文字很好,但每隔2.5秒,圖像顯然會重新加載,導致文字移位。
這裏是JS
window.onload = function(){
inth = setInterval(function(){
if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest();}else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){;
try{z=xmlhttp.responseXML.documentElement.getElementsByTagName("activity");}catch (er){z = "";}
for (i=0;i<z.length;i++){
zz=z[i].getElementsByTagName("time");
{try{timez = zz[0].firstChild.nodeValue;}catch (er){timez = "";}}
zz=z[i].getElementsByTagName("str");
{try{ustr = zz[0].firstChild.nodeValue;}catch (er){ustr = "";}}
zz=z[i].getElementsByTagName("user");
{try{user = zz[0].firstChild.nodeValue;}catch (er){user = "";}}
zz=z[i].getElementsByTagName("act");
{try{act = zz[0].firstChild.nodeValue;}catch (er){act = "";}}
ntxt = ntxt + "<div style='border-bottom: 3px dotted #DBDBDB; padding:.5em;'><div><span style='display:inline-block; font-size:14px; font-weight:bold; line-height:14px; margin:0; padding:0;'><img src='avatar.php?size=14&hash="+ustr+"' style='height:14px; display:inline-block;padding:0;margin:0;' /> "+user+"</span></div><span style='font-weight:bold;'>"+act+"</span><small> - "+timez+"</small></div>";
}
ntxt = ntxt + "</div>";
}
document.getElementById('actstream').innerHTML=ntxt;
}
}
xmlhttp.open("GET","ajax_not.php",true);
xmlhttp.send();
}, 2500);
return false;
}
什麼是我最簡單的方法可以防止圖片來自重裝?
你在說什麼圖片? – jfriend00
你怎麼知道他們沒有被緩存?你使用瀏覽器的網絡工具嗎? –