是否可以使用jQuery从服务器重新加载具有相同文件名的图像?
例如,我在页面上有一个图像,但是,物理图像可以根据用户操作而更改。注意,这并不意味着文件名发生了变化,而是实际的文件本身发生了变化。
ie:
用户在默认页面上查看图像
用户上传新图像
页面上的默认图像没有改变(我假设这是由于文件名相同,浏览器使用缓存版本)
不管下面的代码被调用的频率如何,同样的问题仍然存在。
$("#myimg").attr("src", "/myimg.jpg");
在jQuery文档中,如果“load”函数有一个默认方法来触发事件,而不是将回调函数绑定到成功/完成的元素加载,那么它将是完美的。
非常感谢任何帮助。
为了绕过缓存并避免向图像url添加无限时间戳,在添加新时间戳之前去掉之前的时间戳,这就是我所做的。
//refresh the image every 60seconds
var xyro_refresh_timer = setInterval(xyro_refresh_function, 60000);
function xyro_refresh_function(){
//refreshes an image with a .xyro_refresh class regardless of caching
//get the src attribute
source = jQuery(".xyro_refresh").attr("src");
//remove previously added timestamps
source = source.split("?", 1);//turns "image.jpg?timestamp=1234" into "image.jpg" avoiding infinitely adding new timestamps
//prep new src attribute by adding a timestamp
new_source = source + "?timestamp=" + new Date().getTime();
//alert(new_source); //you may want to alert that during developement to see if you're getting what you wanted
//set the new src attribute
jQuery(".xyro_refresh").attr("src", new_source);
}