Welcome to WebmasterWorld Guest from 23.20.186.146

Forum Moderators: open

Message Too Old, No Replies

Just a simple JS script

What am I doing wrong???

     
4:28 pm on Jan 23, 2002 (gmt 0)

10+ Year Member



Here is the code

<script type="text/javascript">
<!---
var cookieVar = "OneTwo"

If (cookieVar == "OneTwo")
{
document.write ("Great!")
}
elseif (cookVar != "OneTwo")
{
document.write ("Not so Great")
}
//--->
</script>

But I keep getting errors, specifically the "Object Expected".

Thanks for any help offered,
I appreciate it. :)

4:57 pm on Jan 23, 2002 (gmt 0)

10+ Year Member



Found silly me forgot to put ;

at this line
var cookieVar ="OneTwo";

9:38 pm on Jan 23, 2002 (gmt 0)

WebmasterWorld Senior Member txbakers is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You also put "cookVar" instead of "cookieVar" in your else if clause.

that would cause an object error.

9:08 am on Jan 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Javascript is very case sensitive - it should be if not If. You also use elseif; this is invalid JS syntax and should be replaced with else if.

It should work then.

Also, as your if/else if is mutually exclusive you can just write:

if (cookieVar == "OneTwo")
{
document.write ("Great!")
}
else
{
document.write ("Not so Great")
}

3:07 pm on Jan 24, 2002 (gmt 0)

10+ Year Member



thanks everyone. JS is not hard to learn but a pain. I'm used to coding with PHP. Which is a good server-side lang to learn by the way! (;)wink ;)wink) But getting into JS it's like starting all over again. I just keep pulling my hair till I eventually get it right.

OK I did'nt have to rant a rave so much. But sometimes you got to let it off your chest, you know? :)

Xoc

3:49 pm on Jan 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also the braces are optional. The if and else take one line. That line can be made compound by using the braces. But if you have only one line, the braces are optional. That makes it:

if (cookieVar == "OneTwo")
document.write ("Great!")
else
document.write ("Not so Great")

(wmw removes spaces at the start of the line, but you'd want to indent for readability)

4:02 pm on Jan 24, 2002 (gmt 0)

10+ Year Member



This is neat. I'm learning JS from you guys and I understand you all better then some of this darn books I'm reading on it.

Who would have thought JS could be this much FUN!!!
Golly Gosh Gosh

4:07 pm on Jan 24, 2002 (gmt 0)

10+ Year Member



I'm sorry should have put this in my last post.

What book would you guys recommend on JS and why?

I'm not looking for something to simple, cause that usually leaves me thinking too much. Something with too much info is just crazy. I always learn with time. A book that is somewhat in between that would be ideal, just enough info to where I know whats going on, but not so little information that I am left thinking thinking too much about it.

Thanks

4:32 pm on Jan 24, 2002 (gmt 0)

10+ Year Member



I'd recommend leaving in the optional { and }, for readability. It's just to easy to make mistakes by leaving them out.

I'm not really sure who thought this rule would be a good idea, but it's a common rule among a lot of langauges (even strongly typed langauges where they make the least sense).

5:20 pm on Jan 24, 2002 (gmt 0)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I'd recommend the O'Reilly book. The new edition just came out in November and that makes it complete for the most recent standards.

This book is a reference, and not really a tutorial. I think the best way to learn js is to have some cut and paste scripts (the web is full of them - don't miss our generic javascript [webmasterworld.com] thread.) Then use an authoritative reference like O'Reilly to pull the code apart until it clicks for you.

<added>I just checked and the new 4th edition is on back order right now.</added>

6:12 pm on Jan 24, 2002 (gmt 0)

WebmasterWorld Senior Member txbakers is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I use both the O'Reilly Book - Javascript the Definitive Guide and the Javascript fr Professionals by WROX publishing.

Neither book has all the answers I need, so I usually flip between the two.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month