...

View Full Version : Javascript Functions



snakedevil1
07-31-2002, 04:03 PM
Im having trouble writing functions in javascript. I keep getting a line 20 char 9 ; expected error when running this script:

funtion HomeChangerOn()
{
if (currentstate=="home")
{
} else {
document.homebg.src='navigator/home/cellbghm.jpg';
}
}

line 20 is where i declared the function (the first line). I have gone over my tutorial on javascript, and can't see whats wrong with it. Here's where i called the function:
<div id="hidden" style="position:absolute; left:456px; top:140px; width:75px; height:32px; z-index:2;" onMouseOver="HomeChangerOn();" onMouseOut="HomeChangerOut();"><img src="navigator/aboutme/hiddenam.gif" width="79" height="36"></div>

I've gone over several times and can't figure out the problem.

mordred
07-31-2002, 04:22 PM
You've got a typo:

function HomeChangerOn()

would be correct, i.e. a "c" was missing...

Claudio
07-31-2002, 04:26 PM
Hi there,


It looks to me that your code is missing some logic.

currentstate doesn't refer to anything. Try this:


funtion HomeChangerOn(currentstate)
{
if (currentstate=="home")
{
} else {
document.homebg.src='navigator/home/cellbghm.jpg';
}
}

<div id="hidden" style="position:absolute; left:456px; top:140px; width:75px; height:32px; z-index:2;" onMouseOver="HomeChangerOn('home');" onMouseOut="HomeChangerOut();"><img src="navigator/aboutme/hiddenam.gif" width="79" height="36"></div>


Perhaps this may not be your intentions...

good luck with it.

snakedevil1
07-31-2002, 04:35 PM
o ya, i forgot to include the variable decloration:
var currentstate="home";
Claudio, i don't see which typo your talking about. currentstate is supposed to change depending on what the user clicks on. You can see what im talking about and some other errors that came up by going to my homepage: http://snakedevil1.tripod.com/vip3r/main.htm

mordred
07-31-2002, 05:00 PM
Believe me, it *really* is just a typo in your code: Just replace "funtion" with "function" so JavaScript knows you're wanting to define a function, and that should get rid of your initial error.

RadarBob
07-31-2002, 06:12 PM
Also, nothing is done if, in fact, currentstate == "home". If that is your intent then I humbly suggest you rewrite so the intent is clearer, thus:


...
if (currentstate !="home") {
// do some stuff
}


It's simpler because it's no longer a compound IF; and it doesn't "look funny", as in the original, when there are no statements inside the "true" part of the IF.

If you want different mutually exclusive actions based on descrete values of currentstate, then I suggest a switch statement. That makes the code much clearer, and IMHO, easier to debug/maintain than IF / ELSE IF.....



function HomechangerOn (currentstate) {
switch (currentstate) {
case "home":
// do some stuff
break;

case "lost":
// do something else
break;

default:
// alert that currentstate has an unknown value
}
}

snakedevil1
07-31-2002, 07:05 PM
haha, i didnt' c that the c was in bold.
ok thanx for all ur advice, and i didn't c that typo... thanx a LOT



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum