if cookie = ""

S

screechyboy

OKay so im new to Javascript Cookies....

I am able to set and return cookie values but i know want to create an
if statement!

One of my cookies is called "color" and i wish to write a script that
displays a certain graphic if "Color" equals "White" for example.

The code i have so far is...
<script type="text/javascript">
color=getCookie('color')
if (color="White")
{
alert('Color = '+color+'!')
}
</script>

The alert appears when the page loads but ignores the IF statement so
just displays the value of "color" regardless of wether its value is
"White".

Im stuck and google searching is getting me knowhere, can this be
done?

Your help is greaty appreciated!
 
D

David Mark

OKay so im new to Javascript Cookies....

I am able to set and return cookie values but i know want to create an
if statement!

You now want to create an if statement?
One of my cookies is called "color" and i wish to write a script that
displays a certain graphic if "Color" equals "White" for example.

The code i have so far is...
<script type="text/javascript">
color=getCookie('color')
if (color="White")

Use == for comparisons. The single = is for assignments.
 
S

screechyboy

okay changed to...

<script type="text/javascript">
color=getCookie('color')
if (color!=="White")
{
alert('Color = '+color+'!')
}
</script>

Same result returns "color" regardless. Oh and yes i meant "now want
to create an if statement".
 
D

David Mark

okay changed to...

<script type="text/javascript">
color=getCookie('color')

var color = getCookie('color');
if (color!=="White")
{
alert('Color = '+color+'!')}

</script>

Same result returns "color" regardless. Oh and yes i meant "now want
to create an if statement".

What do you mean it returns "color?" Do you mean it alerts "Color =
color?" If so, then either your getCookie function has a problem or
the "color" cookie is actually "color."
 
S

Stevo

okay changed to...

<script type="text/javascript">
color=getCookie('color')
if (color!=="White")
{
alert('Color = '+color+'!')
}
</script>

Same result returns "color" regardless. Oh and yes i meant "now want
to create an if statement".

The easiest way to find out what's going on is to just remove the logic,
like this:

color=getCookie('color');
alert(color);

Then you'll see immediately what's being returned.
 
S

screechyboy

So on a previous page i set the value of the cookie "color", it could
be "Red", "Blue", "Green", "Black" or "White".

When i execute the code above it correctly returns the value of the
cookie "color" be it "Red", "Blue", "Green", "Black" or "White".

The purpose of my if statement is to only display the alert if the
value fo the cookie "color" is "White".

Thus if color=="White"
 
D

David Mark

So on a previous page i set the value of the cookie "color", it could
be "Red", "Blue", "Green", "Black" or "White".

So who are you replying to?
When i execute the code above it correctly returns the value of the
cookie "color" be it "Red", "Blue", "Green", "Black" or "White".

What code?
The purpose of my if statement is to only display the alert if the
value fo the cookie "color" is "White".

Thus if color=="White"

Except that your last posted snippet did the opposite.
 
E

Evertjan.

So on a previous page i set the value of the cookie "color", it could
be "Red", "Blue", "Green", "Black" or "White".

Are you responding to something?

[please always quote on usenet]
 
S

screechyboy

The Code i have is:
<script type="text/javascript">
color=getCookie('color')
if (color!=="White")
{
alert('Color = '+color+'!')
}
</script>

The bottom line is regardless of the line starting with IF it alerts
me! This shouldnt be happening or at least is not what i want to
happen!
 
D

David Mark

The Code i have is:


The bottom line is regardless of the line starting with IF it alerts
me! This shouldnt be happening or at least is not what i want to
happen!

Try this:

color = "White";
if (color!=="White")
{
alert('Color = '+color+'!')
}

You won't get an alert. The lesson is that your getCookie function
never returns "White."
 
S

screechyboy

Try this:

color = "White";
if (color!=="White")
{
alert('Color = '+color+'!')
}

You won't get an alert. The lesson is that your getCookie function
never returns "White."

Your absolutley right, not that i ever doubted you! So now if you dont
mind i need help understanding why it doesnt return anything using
your code:

color = "White";
if (color!=="White")
{
alert('Color = '+color+'!')
}

You have set color to white so why dont we get an alert?
 
H

haroon

Try this:

color = "White";
if (color!=="White")
{
alert('Color = '+color+'!')
}

You won't get an alert. The lesson is that your getCookie function
never returns "White."

This world is going bananas. OP and everyone else who didn't mention
this, you guys need to pickup a book that teaches basics of javascript
and open the chapter that explains the comparison operators. Did
anyone notice this statement "if (color!=="White")"?
 
S

screechyboy

This world is going bananas. OP and everyone else who didn't mention
this, you guys need to pickup a book that teaches basics of javascript
and open the chapter that explains the comparison operators. Did
anyone notice this statement "if (color!=="White")"?- Hide quoted text -

- Show quoted text -

I dont have a book at present but clearly stated im a newbie to
javascript at the top of this thread, enlighten me please and i
promise i'll go read a book!
 
D

David Mark

Your absolutley right, not that i ever doubted you! So now if you dont
mind i need help understanding why it doesnt return anything using
your code:

color = "White";
if (color!=="White")
{
alert('Color = '+color+'!')

}

You have set color to white so why dont we get an alert?- Hide quoted text -

- Show quoted text -

You changed the comparison operator in mid-thread. I mentioned this
previously.
 
T

Thomas 'PointedEars' Lahn

One of my cookies is called "color" and i wish to write a script that
displays a certain graphic if "Color" equals "White" for example.

The code i have so far is...
<script type="text/javascript">
color=getCookie('color')

Always declare your variables:

var color = getCookie("color");
if (color="White")

That is an *assignment* to `color', overwriting the previously initalized value.
{
alert('Color = '+color+'!')
}
</script>

The alert appears when the page loads but ignores the IF statement

Actually, it does not. Since "White" which becomes the value of `color' is
a true-value, the associated block statement is always executed.
so just displays the value of "color" regardless of wether its value is
"White".

The equals operator in ECMAScript implementations is

==
[...]
Im stuck and google searching is getting me knowhere,

Firefox' error console with enabled JavaScript warnings would have.
can this be done?

Of course.


PointedEars
 
D

David Mark

I dont have a book at present but clearly stated im a newbie to
javascript at the top of this thread, enlighten me please and i
promise i'll go read a book!

He can't enlighten you as he is confused. I suspect he saw !== and
had the typical newb reaction of "too many equal signs." You should
read up on comparison operators though as you seem to have less than a
firm grasp on them.
 
H

haroon

No, I think you have gone bananas. Re-read the thread and try again.

Well to be honest, i didn't read your post in which you said "!==" is
wrong and "!=" is right. In your first reply you explained the
difference between "=" and "==" in your second post you were confused
about what the OP wants in your third post you told the OP that he was
doing opposite of color=="White which is color!="White which he was
_not_ doing and in your fourth post, you made the same mistake
yourself. And in your fifth post you are telling me that I have gone
bananas?
 
H

haroon

haroon said the following on 9/26/2007 5:24 PM:





Have you not noticed the several replies correcting that comparison?
Maybe you need to pickup the group FAQ and give it a good reading.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

May be you need to open up your eyes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top