Friday, July 20, 2007

Forcing an image to refresh

http://www.codingforums.com/showthread.php?t=35507
<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;
if(!isNew.match('#')){
document.images['noCache'].src = myImage.src+'?'+utctime;
}
else{document.images['noCache'].src = document.images['noCache'].src;
}
}
//-->
</script>
</HEAD>

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