Jquery :Force Browser to Reload Images


when you upload image using ajax request sometimes people might have observed the problem that image will not get updated

To make the changes effect you have to refresh the page ,instead of refreshing the image you can get the updated image using the following way

Just add a current time or a random number to the image path as shown below.

d = new Date();
$("#myimg").attr("src", "/myimg.jpg?"+d.getTime());


The reason we don't get the updated images are,the browser caches all the images,so it need not to got to server back if the image path is same, as your image path is same it will get the cached image from the browsers cache,so ypu don't see any change in the image

So to overcome that just  add time to that,since the new image name is not there in cache the browser always gets the image from the server.

No comments:

Post a Comment