View Full Version : Forcing an image to refresh

03-22-2004, 02:43 AM
Hello, I have a website that shows images off of an ftp server that my computer has set up. What I need to know is how can I force the browser to redownload an image? For instance, my ftp server has a file called: "Welcome.jpg" and every minute the jpg is changed to be something else, but it is still named "Welcome.jpg". When I try to use javascript to set the source of the image, the image isnt' re-downloaded because it has the same path as before. Any ideas as to how I could force the browser to redownload the image instead of looking in its cache? Thanks a lot.

Willy Duitt
03-22-2004, 03:19 AM
This script will add a querry (?) and the UTC time to the end of the image URL.
Resulting in a unique image URL according to the date and time it is downloaded.

<script type="text/javascript">
function noCache(){
var myImage = new Image();
myImage.src = 'Welcome.jpg';
var clocktime = new Date();
var utchours = clocktime.getUTCHours();
var utcminutes = clocktime.getUTCMinutes();
var utcseconds = clocktime.getUTCSeconds();
var utcyear = clocktime.getUTCFullYear();
var utcmonth = clocktime.getUTCMonth()+1;
var utcday = clocktime.getUTCDate();
var utctime = utcyear+''+utcmonth+''+utcday;
utctime += utchours+''+utcminutes+''+utcseconds;

isNew = myImage.src;
document.images['noCache'].src = myImage.src+'?'+utctime;
else{document.images['noCache'].src = document.images['noCache'].src;

<BODY onload="noCache()">
<img name="noCache" src="Welcome.jpg" width="" height="" alt="">


11-09-2005, 05:23 AM
I've been using Willy's autorefresh script for a while now and it works great!

Now I'd like to implement this together with a rollover script because I'd like to update several thumbnail pictures with new ones but the file names stay the same.

The problem is that all rollover scripts which I have found so far use only one image tag...

As I understand your script, the "original" and the "over" images would need *separate* IDs so that they would autorefresh.

Does anybody know if there is a way to do an autorefresh of rollover images without having to resort to CSS based rollovers?

How to do this?

Thanks, martin

12-03-2005, 12:15 AM
I would suggest creating a hidden image with a src that is equal to that of the rollover image. Try this and see if it helps, I have not tried it but it might be a good starting point. Good luck!