Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Functions

    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.
    Computers are simple for the complex mind, yet complex for the simple mind

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Javascript Functions

    You've got a typo:

    function HomeChangerOn()

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

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Humm

    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.

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by snakedevil1; 07-31-2002 at 05:42 PM.
    Computers are simple for the complex mind, yet complex for the simple mind

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A coding Technique Suggestion

    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:
    Code:
    ...
    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.....

    Code:
    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
       }
    }

  • #7
    New Coder
    Join Date
    Jun 2002
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Computers are simple for the complex mind, yet complex for the simple mind


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •