简体   繁体   English

javascript / jquery测试图片网址进行更新

[英]javascript/jquery test image URL for update

Is there a way though the DOM to test an image URL to see when it was last updated? 通过DOM是否可以测试图像URL以查看其最近更新时间?

I am uploading an image to the web server. 我正在将图像上传到Web服务器。 The image will replace a previous image with the same URL. 该图像将用相同的URL替换先前的图像。 I'm using plupload via ajax. 我正在通过ajax使用plupload。 When the upload is done, I want to update the image on the page. 上传完成后,我想更新页面上的图像。

I have tried using the FileUploaded event to trigger updating the src on the image. 我尝试使用FileUploaded事件来触发更新映像上的src。 But I think it's either pulling the image from cache or it's firing faster than the web server can server the new image. 但是我认为它要么从缓存中拉出图像,要么其发射速度比Web服务器可以存储新图像的速度快。

I know I could hack it by sleeping a couple of seconds. 我知道我可以睡几秒钟来破解它。 But I can think of all kinds of reasons why that would fail. 但是我可以想到导致失败的各种原因。

Is there a way to read a header from the file to tell if the file has changed on the server? 有没有一种方法可以从文件中读取标头,以判断服务器上的文件是否已更改? Then I can keep asking until it does? 然后我可以继续询问直到它呢?

A more elegant solution would of course be better. 当然,更优雅的解决方案会更好。

Thanks 谢谢

Since you say you're loading the image from the same URL, you need to trick the browser so it doesn't get the image from the cache. 因为您说要从相同的URL加载图像,所以您需要欺骗浏览器,以便它不会从缓存中获取图像。 Appending the time should do it... 花费时间应该做...

$("img").attr("src", "/image.png?t=" + (new Date().getTime()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM