...

View Full Version : Force automatic image reload onload.



codegeek
05-05-2007, 05:45 AM
Hi guys, I'm not too familiar with Javascipt and in fact had attempted to solve this problem with PHP initially. Here is my objective:

I want to have a page that constantly reloads an image as soon as it's loaded. I understand I can make the image seem "new" to the client by adding on a variable such as image.gif?num=i (Within a for loop with variable i) but I am not sure how I can get it to initially load the image, wait until it is loaded, and then once it is reloaded empty the contents of the DIV it is in and reload the image again with a new ?num.

Could someone help me out with this?

rwedge
05-05-2007, 07:20 AM
Something like this should work:
<script type="text/javascript">
var imgfile = 'http://www.somewhere.com/images/pic.jpg';
function dejaView() {
var now = new Date();
var i = now.getTime();
myImage = new Image();
myImage.src = imgfile+'?'+i;
ckLoad();
}
function ckLoad() {
if (myImage.complete) {
document.getElementById('myimg').innerHTML='<img src="'+myImage.src+'" alt="" title="" />';
dejaView(); }
else { setTimeout('ckLoad()', 100); }
}
onload=dejaView;
</script>

<div id="myimg"> </div>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum