PDA

View Full Version : Error in Array and doback in JavaScript


ammy
04-05-2006, 01:05 AM
Hello! I am a noob at JavaScript who wants to enhance her knowledge by joining forums. I do online tutorials, to gain more knowledge about JavaScript. Upon on doing a pretty simple one (to the advanced users out there, of course) my code had an error. Because firefox tells me the error, I know what it is, but I am not able to fix this error. The script is listed below:


<HTML>
<HEAD>
<TITLE>HTML and JavaScript</TITLE>
<SCRIPT>
var imgArray = newArray(8);
imgArray[0] = New Image;
imgArray[0].src = "candles.jpg";
imgArray[1] = New Image;
imgArray[1].src = "garden.jpg";
imgArray[2] = New Image;
imgArray[2].src = "kungfu.jpg";
imgArray[3] = New Image;
imgArray[3].src = "floss.jpg";
imgArray[4] = New Image;
imgArray[4].src = "happyhour.jpg";
imgArray[5] = New Image;
imgArray[5].src = "slipprs.jpg";
imgArray[6] = New Image;
imgArray[6].src = "incence.jpg";
imgArray[7] = New Image;
imgArray[7].src = "hampers.jpg";
var index= 0;

function doBack()
{
if(index > 0)
{
index--;
document.slideshow.src = imgArray[index].src;
}
return;
}

function doNext()

if (index < 8)
{
index++;
document.slideshow.src = imgArray[index].src;
}
return;

}

</SCRIPT>
</HEAD>
<BODY>
<CENTER>
<H2>My JavaScript Slide Show</H2>
<P>
<IMG NAME ="slideshow" SRC="candles.jpg">
<P>
<A HREF="javascript:doBack()">Back</A>
&nbsp;&nbsp;
&nbsp;
<A HREF="Javscript:doNext()">Next</A>
</CENTER>
</BODY>
</HTML>




Firefox is telling me that doback is not defined and imgArray[0] = New Image; is missing something before statement, I tried to add something, but that did not fix the problem. Please help me to learn more about JavaScript, and this error! Thank you guys :thumbsup:

BaldEagle
04-05-2006, 05:00 AM
I believe your problem is stemming from New. I think it is case sensitive so try new instead. This may be causing javascript to abort and therefore doBack is undefined.

BaldEagle

Kor
04-05-2006, 09:05 AM
You can do it simplier:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var pic=['candles','garden','kungfu','floss','happyhour','slipprs','incence','hampers'];
var a=0;
function slide(q){
a=(a==pic.length-1&&q==1)?0:a+q;
a=(a==-1)?pic.length-1:a;
document.getElementById('slideshow').setAttribute('src',pic[a]+'.jpg');
}
</script>
</head>
<body>
<img id="slideshow" src="candles.jpg">
<a href="#" onclick="slide(-1);return false">Back</a>&nbsp;&nbsp;&nbsp;<a href="#" onclick="slide(1);return false">Next</a>
</body>
</html>

ammy
04-05-2006, 11:04 PM
Kor: Thanks a lot, your code works great:thumbsup: .. too bad I don't really understand it. :confused:

Baldeagle:I have tried chaning the letter to lowercase.. but the problem still persists.. Thanks for helping anyway:o

Kor
04-06-2006, 08:58 AM
too bad I don't really understand it.

var pic=['candles','garden','kungfu','floss','happyhour','slipprs','incence','hampers'];

This is an array in JSON (http://www.json.org/)notation

var a=0;

this would be the index for the array' elements

a=(a==pic.length-1&&q==1)?0:a+q;

This is the ternary operator. It is a shorthand for

if(a==pic.length-1 && q==1){
a=0;//reinitialize the index if it arrives at the end of the array's elements
}
else{
a=a+q;//increse/decrese the index
}

the ternary looks like

condition?if_true:if_false

document.getElementById('slideshow').setAttribute('src',pic[a]+'.jpg');

it is the DOM 1 equivalent for

document.getElementById('slideshow').src=pic[a]+'.jpg';

felgall
04-07-2006, 01:09 AM
This is an array in JSON (http://www.json.org/)notation

It is also a valid way to define an array in Javascript (JSON just uses the shortest Javascript equivalent for each of its notations and is not really relevant to a discussion about Javascript).

The original code may also not be working because it is using href="javascript:xxx()" instead of href="#" onclick="xxxx();return false" using javascript in an href often causes things to screw up since it isn't actually triggering a Javascript event.