开发者

Load and replace image with same name - load and cache image problem

开发者 https://www.devze.com 2023-01-07 13:02 出处:网络
I am trying to load a image from the server (same name but always a new image) into the html page - tag: #webCamStageImage. So far I can load the image but the browser doesn\'t show me the new image -

I am trying to load a image from the server (same name but always a new image) into the html page - tag: #webCamStageImage. So far I can load the image but the browser doesn't show me the new image - I guess it's a caching problem - because when I turn on "disable WebCore Cache" in Safari it works.

Heres my code:

setInterval(function()
{
  $("#webCamStageImage").a开发者_StackOverflowttr({'src': "http://picture.jpg"});
}, 5000);

How can I change/ddelte/refresh the old image?

chris


Try using a URL with a nonsense query string tacked onto it:

setInterval(function()
{
  $("#webCamStageImage").attr({'src': "http://picture.jpg?foo=" + new Date().getTime()});
}, 5000);

The browser treats the whole URL as the name of a cacheable entity, so by doing this you make it think that you're asking the server for something different (which indeed you might be). The server won't pay any attention to the query string (unless you know otherwise ...).


Try adding random value to query string value so as to avoid caching:

var randNum = Math.floor(Math.random() 999 + 1);
setInterval(function()
{
  $("#webCamStageImage").attr({'src': "http://picture.jpg?rand=" + randNum});
}, 5000);
0

精彩评论

暂无评论...
验证码 换一张
取 消