0

Is it possible to name a cookie in javascript?

i've got a web program that will display

"name:undefined Name:Jack Grade:32"

whenever i display the contents of a cookie i create. i'm assuming th first bit is the cookie name itself and then the other two bits are the contents?

also, when i try to add a new bit to the cookie it just overwrites it, is their a way i can add it to the end of this one. so it displays

name:undefined Name:Jack Grade:32
name:undefined Name:Billy Grade:72

???

here is the code i have

<script type="text/javascript">
function WriteCookie()
{

	var allowed=/^[a-zA-Z]+$/;

	if(document.gradeAdd.studentName.value.match(allowed))
	{

		cookievalue=escape(document.gradeAdd.studentName.value)+";";
		cookievalue2=escape(document.gradeAdd.studentGrade.value)+";";
		document.cookie="Name = " + cookievalue;
		document.cookie="Grade = " + cookievalue2;
		alert(document.cookie);
	}
	else
	{
		alert("Enter a valid name");
		return;
	}
}

function ReadCookie()
{
   var allcookies = document.cookie;
   // Get all the cookies pairs in an array
   cookiearray  = allcookies.split(';');

   // Now take key value pair out of this array
   for(var i=0; i<cookiearray.length; i++)
  {
      name = cookiearray[i].split('=')[0];
      value = cookiearray[i].split('=')[1];
	  document.write(name + ":" + value);
  }
}
</script>
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Atomika3000
0

Is it possible to name a cookie in javascript?

i've got a web program that will display

"name:undefined Name:Jack Grade:32"

whenever i display the contents of a cookie i create. i'm assuming th first bit is the cookie name itself and then the other two bits are the contents?

also, when i try to add a new bit to the cookie it just overwrites it, is their a way i can add it to the end of this one. so it displays

name:undefined Name:Jack Grade:32
name:undefined Name:Billy Grade:72

???

here is the code i have

<script type="text/javascript">
function WriteCookie()
{

	var allowed=/^[a-zA-Z]+$/;

	if(document.gradeAdd.studentName.value.match(allowed))
	{

		cookievalue=escape(document.gradeAdd.studentName.value)+";";
		cookievalue2=escape(document.gradeAdd.studentGrade.value)+";";
		document.cookie="Name = " + cookievalue;
		document.cookie="Grade = " + cookievalue2;
		alert(document.cookie);
	}
	else
	{
		alert("Enter a valid name");
		return;
	}
}

function ReadCookie()
{
   var allcookies = document.cookie;
   // Get all the cookies pairs in an array
   cookiearray  = allcookies.split(';');

   // Now take key value pair out of this array
   for(var i=0; i<cookiearray.length; i++)
  {
      name = cookiearray[i].split('=')[0];
      value = cookiearray[i].split('=')[1];
	  document.write(name + ":" + value);
  }
}
</script>

There is a brilliant guide to cookie work available at http://www.quirksmode.org/js/cookies.html.

For me, cookies have only worked when I've done:

document.cookie = "cookieName=cookieValue; expires=Mon, 4 Apr 2011 14:16:10 UTC; path=/"

so you could try:

document.cookie = "Name=Joe; expires=Mon, 4 Apr 2011 14:16:10 UTC; path=/"

. YMMV though and I recommend familiarising yourself with the Quirksmode stuff.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.