Jump to content

Hi- I need a javascript equvalent to this php function for cookie's


antonyfal

Recommended Posts

I have a problem running php on some of my html files-- I need a javascript equivalent of this little snipet, it checks if there is a cookie, if there is i can run a div, if there is no cookie then it will run the other div-- Everything i tried didn't work... here is the PHP version:

this file is run on a .html file.

 

<?php if(!isset($_COOKIE['letsChat'])){?>

<div style="width:89px; height:19px" ><a href="javascript:void(0);" onClick="dhtmlwindow.open('{EMAIL2}', 'iframe', 'http://xxxxxx.com', '{EMAIL2}', 'width=224px,height=237px,resize=0,scrolling=0,center=1');"><font color="#ff0033"><b>CHAT ONLINE</b></font></a></div>

                        <?php } else { ?>

<div style="width:89px; height:19px; background-color:transparent;"><font color="#ffffff"><b>CHAT OFFLINE</b></font><div>

<?php } ?>

 

 

// to note: the cookie was set using this code:

 

<script language="javascript">

document.cookie = "ShowFile= 1;"

</script>

 

// the value of the cookie is not to important but useful to know how to get/check it..

// is on a html file which is a pat-template file.. so no doctype headers --

 

regards tony

//sorry i mean this script:

<script language="javascript">

document.cookie = "letsChat= 1;"

</script>

 

this is probably easy for someone out there, i think you looking at it saying!! "why bother", but my javascript is like my Chinese--- just not there!

You set you cookie like so:

The semicolons needs to be outside the quotes

document.cookie = "someValue=1"; 

 

then you get it with this function

function isCoockieSet(value) {
	var foo = document.cookie.split("=");
	for (key in foo) {
		if (foo[key] == value) { return true };
	}
return false;
}

 

You call the function like so:

isCookieSet('YourValue')

in this case someValue.

 

The func will return true if cookie is there. False otherwise.

Just remember to pass a string to isCookieSet

 

 

PS:

i think you looking at it saying!! "why bother", but my javascript is like my Chinese--- just not there!

 

lol dude chill out.

Ta! Omirion.

I got a good idea how it should work from your code.. nice example ;D

 

just this part:

 

isCookieSet('YourValue')  ???

How can i write it into the code above? if true return first div, otherwise return other div?

 

 

Hey i tell you!! the simplest thing, can get so complex!! like this little thing has set me back 3 days, and when i get it will boost me to nearly finished:D..,

I'm new at cookies but i just learn't the value of them and how useful and simplistic they are to apply... I would say that this is cookies 1 o 1.. 

 

if you or someone can show how to apply this directly to the php example above so i can see it in action with the html in between.. Then all my pieces of puzzle will fall together!! and you will be responsible:'D..

Thanks for this reply..

Tony

 

Sorry your code i way to jumbeled for me to sift trough.

 

You need to pass the value you put in the cookie to isSetCookie.

Thank proceed as you explained.

 

if(isSetCookie("value")){do whatever} else {do something else}

 

Also your jedi mind tricks will not work on me :), i'm from eastern Europe, we don't buy into theese things.

Good luck and all...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.