用javascript重新加载缓存的图像 [英] Reload a cached image with javascript

查看:316
本文介绍了用javascript重新加载缓存的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用javascript重新加载已缓存的特定图像?

is it possible to use javascript to reload a particular image that is has cached?

在我的网站上,用户可以上传新的头像.完成此操作后,我希望浏览器重新下载新的头像,以便用户可以看到已进行更改.

On my site users can upload new avatars. After this is done, I want the browser to redownload the new avatar, so the user can see that the change is made.

此刻,我正在这样做:

$('profilePic').src = 'flash/userImage.ashx?id=12345677&type=avatar&t=' + new Date().getTime()

userImage.ashx返回基于id和查询类型的图像.它不使用't'参数-只是URL有所不同,因此图像会重新加载.

userImage.ashx returns images based on id and queried type. It doesn't use the 't' parameter - this is just so the url is different, so the image reloads.

这在用户上传其新头像的页面上运行良好,但是在网站上的其他地方,它仍将使用缓存的图像.

This works fine on the page where the user uploads their new avatar, but elsewhere on the site it will still use the cached image.

我可以关闭用户头像的缓存,但是我真的不想这样做.是否可以强制重新加载特定的缓存图像?

I could turn off caching for user avatars, but I don't really want to do this. Is it possible to force a particular cached image to reload?

推荐答案

也许使用ETag进行缓存是一种解决方案: http://en.wikipedia.org/wiki/HTTP_ETag

Maybe caching with ETag is a solution: http://en.wikipedia.org/wiki/HTTP_ETag

当头像更新后其他用户加载页面时,他随图像请求发送的ETag与服务器上生成的ETag不同,因此应导致200响应而不是304,因此图像重新加载.

When a different users loads a page after the avatar update, the ETag he sends with the request for the image is different than the ETag generated on the server and should therefore result in a 200 response instead of a 304, so the image is reloaded.

这篇关于用javascript重新加载缓存的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆