jquery .load函数,IE中的缓存失败

时间:2012-01-16 18:00:10

标签: jquery image internet-explorer load

我正在使用jquery“.load”功能来延迟图片的“淡入淡出”,直到它们被加载。 然而它在所有浏览器中工作正常,只是IE正在制造麻烦 - 它是缓存......到目前为止我理解了这个问题......但是如何解决呢?有插件可以解决这个问题吗?

这里有一个代码示例:

// this is the user thumbnail, which will be loaded from the database, with a random number as filename.
if (thumb_medium == '1') {  

    // create picture link.
    var pic = Math.floor( Math.random() * (1000000) );
    var image_link = "image/image_show.php?id=" + id + "&element=image_profile_thumb_medium" + "&pic=" + pic + '.jpg';

    // write the picture link to the src attr.
    $('#brpr_list_thumb_' + tuple).attr('src', image_link); 
}
else {

    // male image - if there is no user picture in the database.
    if (gender == 'male') { var image_link = "pic/icons/male_128_128.png" };

    // female image - if there is no user picture in the database.
    if (gender == 'female') { var image_link = "pic/icons/female_128_128.png" };

    // write the link to the src attr.
    $('#brpr_list_thumb_' + tuple).attr('src', image_link); 
}


// when the loading process of the picture is finished a fadeIn happens - this makes problems in IE, but just with the male and female image, which get cached from IE.
$('#brpr_list_thumb_' + tuple).load(function() { $('#brpr_list_thumb_' + tuple).fadeIn(250) });

在这个例子中,男性和女性的图像被IE缓存,下次加载时它们不会出现!...

解决问题的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

IE非常有用,默认情况下缓存ajax返回!

如果您想获得合理的回复,则可以手动将所有ajax请求设置为未缓存。把它放在脚本的顶部:

$.ajaxSetup({
  cache: false
});

答案 1 :(得分:0)

您可以为图片网址添加时间戳。试试这个

if (thumb_medium == '1') {  

    // create picture link.
    var pic = Math.floor( Math.random() * (1000000) );
    var image_link = "image/image_show.php?id=" + id + "&element=image_profile_thumb_medium" + "&pic=" + pic + '.jpg';

    // write the picture link to the src attr.
    $('#brpr_list_thumb_' + tuple).attr('src', image_link + "&t=" + (new Date()).getTime()); 
}
else {

    // male image - if there is no user picture in the database.
    if (gender == 'male') { var image_link = "pic/icons/male_128_128.png" };

    // female image - if there is no user picture in the database.
    if (gender == 'female') { var image_link = "pic/icons/female_128_128.png" };

    // write the link to the src attr.
    $('#brpr_list_thumb_' + tuple).attr('src', image_link + "?t=" + (new Date()).getTime()); 
}