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
https://forums.phpfreaks.com/topic/154125-js-cookie-getvalue/
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
https://forums.phpfreaks.com/topic/154125-js-cookie-getvalue/#findComment-810472
Share on other sites

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.