Jump to content

JS Cookie getvalue


abch624

Recommended Posts

Hi All,

 

I have a few javascript functions bellow

<script type="text/javascript">
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
	{ 
	c_start=c_start + c_name.length+1; 
	c_end=document.cookie.indexOf(";",c_start);
	if (c_end==-1) c_end=document.cookie.length;
	return unescape(document.cookie.substring(c_start,c_end));
	} 
  }
return "";
}
</script>
<script type="text/javascript">
function cookieWhatWeSay(name)
{
	cookie=getCookie(name);
	if (cookie!=null)
	{
		var value = cookie.GetCookieValue();
		if (value == "inline")
		{
			document.getElementById('whatwesay1').style.display = 'inline';
			document.getElementById('whatwesay2').style.display = 'none';
			setCookie('whatwesay','inline',365);
		}
		else
		{
			document.getElementById('whatwesay1').style.display = 'none';
			document.getElementById('whatwesay2').style.display = 'inline';
			setCookie('whatwesay','none',365);
		}
	}
}
</script>

</head>
<body onLoad="cookieWhatWeSay('whatwesay')">

 

The setCookie function works perfectly but the one I am interesetd in is cookieWhatWeSay(name) this works partially:) I need to  know how I can get the value from the cookie. Basically the cookie can have two values (i.e. inline or none) How do I get this value from the cookie and then use the if statements in the cookieWhatWeSay(name) function.

 

Hope you understand what I say:)

 

Please help.

 

Cheers - Zahid

Link to comment
Share on other sites

Something like this, maybe?

 

function getCookie(name) {
var cookies = document.cookie.split("; ");
for(var i = 0; i < cookies.length; i++) {
	var cookie = cookies[i].split("=");
	if(cookie[0] == name) {
		return cookie[1]; // perhaps unescape this
	}
}
return false;
}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.