Javascript sideshow

This slideshow works, but I had to put a conditional statement in where it shouldn’t be needed. I did this because the variable imgSize returns a zero the first time the page loads. Does anyone know the reason for this? thanks.

if(!imgindex==0)
{
centerAd(imgSize)
}else{
centerAd(firstimage)
}




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script language="JavaScript1.1">
<!--

//resize event
window.onresize = function(){centerAd();}

//sideshow function
var slideimages=new Array()

function slideshowimages()
{
	for (i=0;i<slideshowimages.arguments.length;i++)
	{
	slideimages[i]=new Image()
	slideimages[i].src=slideshowimages.arguments[i]
	}
}


//center function.
function centerAd(imgSize)
{
 var sw;
 if(parseInt(navigator.appVersion)>3)
 {
  if(navigator.appName=="Netscape"){sw = window.innerWidth;}
  if(navigator.appName.indexOf("Microsoft")!=-1){sw = document.body.offsetWidth;}
  }
 var w = (sw-imgSize)/2;
 document.getElementById("slideshowContainer").style.width = sw+"px";
 document.getElementById("slideshow").style.marginLeft = w+"px";
 }

//-->
</script>
<style type="text/css">
body { margin: 0; padding: 0; }

#slideshowContainer
{
/*width set by javascript above */
/*screen width is detected on the fly */
position:relative;
overflow:hidden;
background-color:#181818;
}
#slideshow
{
/*margin-left set by javascript above*/
/*nessesary margin is calculated on the fly*/
width:2680px;
}


</style>
</head>
<body > 

<!-- first image to be displayed, image is modified by the below code to display a new image every X number of milliseconds -->
<div id="slideshowContainer">
	<div id="slideshow">
	<!--src, because sideit() runs through once and imgindex=0, so first image will be displayed but left in becuse Active-X message-->
		<img src="Adone.jpg" border="0" name="slide" height=481 >
	</div>
</div>



<script>
<!--
//(1)Call sideshowimages function, preload images.
slideshowimages("adone.jpg","adtwo.jpg","adthree.jpg","adfour.jpg")


//configure the speed of the slideshow, in miliseconds
var slideshowspeed=2000
var imgindex=0
var firstimage=2680
function slideit()
{
//if browser does not support the image object, exit.
	if (!document.images){
		return
	}

		//core of code, changes the image.
		document.images.slide.src=slideimages[imgindex].src
		
		//crop,center image
		//this calls the centerAd function, passing it argument imgSize.
		var imgSize=slideimages[imgindex].width
		if(!imgindex==0)
		{
		centerAd(imgSize)
		}else{
		centerAd(firstimage)
		}
		
	//if imgindex is not at the last image increment, else reset imgindex to zero.
	if (imgindex<slideimages.length-1){
		imgindex++
	}
	else{
		imgindex=0
		}
		//call slideit function agian after some time.
	setTimeout("slideit()",slideshowspeed)
	
}

//(2)
slideit()

//-->
</script>

</body>
</html>



That’s quite likely due to the fact that the image is still loading, so the page has no idea of how big the image will be until it’s finished loading.

Do you want to wait until the image has finished loading?

humm, I guess that could be. But I thought since the images were preloaded I could get size right away. But I’m not against adding a wait.

You could always check the complete property of the image, which in your case would be:
slideimages[imgindex].complete

If it’s not set, you know that the image hasn’t completed loading, at which point you can setup a setTimeout event to run the function again.

well that is an excellent suggestion! Thank you very much for your comments, if I have any luck getting to the bottom of this I will post back.