AJAX Prevent Images from being loaded over and over?
So I have a simple AJAX request (not JQuery):
function ajaxfunction(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser sucks"); //error
return false;
}
}
}
ajaxRequest.open("GET", 'pull.php?ms='+new Date().getTime(), true);
ajaxRequest.send(null);
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
}
And I want the request to also pull pictures. Since the request is dynamic (the images shown will change) I do the request over and over. This, however, makes the images flicker. I tried <img src="foobar.jpg" style="visibility:none;" onLoad="this.style.visibility='visible';" />
but that doesn't really help. If anybody开发者_StackOverflow社区 knows of any fix, thank you in advance. :D
Maybe instead of pulling then entire <img>
tag in the ajax response just get the image name and properties to change the targeted image src
, height
, width
, etc... properties to use the new image.
I wonder if including the image height and width in the style attribute of the img tag would help since it takes a split second for the browser to start downloading the image and get the actual image height and width.
精彩评论