robski_g
12-19-2006, 04:44 PM
Hi,
I am fairly new to Javascript and require some help. I would be very appreciative if someone could take the time to explain how the following script works. I understand the first part about defininng the variables and images but i am unsure as to how the array and functions work. If someone could explain what the various parts of the code do I would be most greatful.
<html>
<head>
<script type="text/javascript">
<!--
var pic_width=100;
var pic_height=100;
var button_text="Next Image";
if (document.images)
{
pic1= new Image(pic_width,pic_height);
pic1.src="image1.jpg";
pic2= new Image(pic_width,pic_height);
pic2.src="image2.jpg";
pic3= new Image(pic_width,pic_height);
pic3.src="image3.jpg";
pic4= new Image(pic_width,pic_height);
pic4.src="image4.jpg";
pic5= new Image(pic_width,pic_height);
pic5.src="image5.jpg";
}
var pics= new Array(5)
pics[0]=pic1.src;
pics[1]=pic2.src;
pics[2]=pic3.src;
pics[3]=pic4.src;
pics[4]=pic5.src;
var numpics=5;
var thenum=0;
imgName="img1";
function change_it()
{
if (document.images)
{
document.write("<IMG SRC='"+pics[thenum]+"' border='0' width='"+pic_width+"' height='"+pic_height+"' name='img1'>\n");
document.write("<BR><FORM><INPUT TYPE='button' value='"+button_text+"' onClick='change_it2()'></FORM>");
}
}
function change_it2()
{
var x=0;
thenum+=1;
if (thenum>numpics-1)
{
document[imgName].src=pics[0];
thenum=0;
}
else
{
document[imgName].src=pics[thenum];
x+=1;
}
}
//-->
</script>
<script type="text/javascript">
<!--
change_it();
//-->
</script>
</head>
<body>
I am fairly new to Javascript and require some help. I would be very appreciative if someone could take the time to explain how the following script works. I understand the first part about defininng the variables and images but i am unsure as to how the array and functions work. If someone could explain what the various parts of the code do I would be most greatful.
<html>
<head>
<script type="text/javascript">
<!--
var pic_width=100;
var pic_height=100;
var button_text="Next Image";
if (document.images)
{
pic1= new Image(pic_width,pic_height);
pic1.src="image1.jpg";
pic2= new Image(pic_width,pic_height);
pic2.src="image2.jpg";
pic3= new Image(pic_width,pic_height);
pic3.src="image3.jpg";
pic4= new Image(pic_width,pic_height);
pic4.src="image4.jpg";
pic5= new Image(pic_width,pic_height);
pic5.src="image5.jpg";
}
var pics= new Array(5)
pics[0]=pic1.src;
pics[1]=pic2.src;
pics[2]=pic3.src;
pics[3]=pic4.src;
pics[4]=pic5.src;
var numpics=5;
var thenum=0;
imgName="img1";
function change_it()
{
if (document.images)
{
document.write("<IMG SRC='"+pics[thenum]+"' border='0' width='"+pic_width+"' height='"+pic_height+"' name='img1'>\n");
document.write("<BR><FORM><INPUT TYPE='button' value='"+button_text+"' onClick='change_it2()'></FORM>");
}
}
function change_it2()
{
var x=0;
thenum+=1;
if (thenum>numpics-1)
{
document[imgName].src=pics[0];
thenum=0;
}
else
{
document[imgName].src=pics[thenum];
x+=1;
}
}
//-->
</script>
<script type="text/javascript">
<!--
change_it();
//-->
</script>
</head>
<body>