This is a quicktip on how to update images on a webpage that retain the same filename but is periodically updated, without having to perform a full page load.
The
function updateImage(src) {
if(src.indexOf('?') > -1) {
src = src.substr(0, src.indexOf('?'));
}
return src + '?' + (new Date()).getTime();
}
var img = document.getElementById('myImage');
img.src = updateImage(img.src);
You could theoretically add a hash (#) after the filename instead of a querystring (?), but unfortunately that doesn’t work in Internet Explorer. So stick with the querystring and you will be fine.
April 1, 2009 at 7:47 am
Just watch out to bogus access data when using access log traffic analyzers like Awstats.
PS: A typo: cashed -> cached — I know you wanna cash, I want it too!
April 1, 2009 at 9:31 am
Thanks Jan, and yeah, I want lots of cash!! 🙂
May 15, 2009 at 2:07 am
so useful approach, thanx gabriel..