Print cookie values only

I am writing a program that stores information into cookies based on user input and then allows the user to search through the cookies for matching values. Everything works fine, except I need it to print the cookie values only and without the commas in between (the cookie information is stored into an array) when someone does a search. Can someone help? Thanks in advance.
-John
<html>
<head>
	<title>Cookies</title>
</head>
 
<script language="JavaScript">
 
	var cookieCounter = 1
 
	function getCookieValue(cookieName)
	{
		   var cookieValue = document.cookie;
		   var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
 
		   if (cookieStartsAt == -1)
		   {
		      cookieStartsAt = cookieValue.indexOf(cookieName + "=");
		   }
 
		   if (cookieStartsAt == -1)
		   {
		      cookieValue = null;
		   }
		   else
		   {
		      cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
		      var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
		      if (cookieEndsAt == -1)
		      {
		         cookieEndsAt = cookieValue.length;
		      }
		      cookieValue = unescape(cookieValue.substring(cookieStartsAt,
		         cookieEndsAt));
		   }
 
		   return cookieValue;
		}
 
 
	function setCookie(cookieName, cookieValue, cookiePath, cookieExpires)
	{
	  	   cookieValue = escape(cookieValue);
 
		   if (cookieExpires == "")
		   {
		      var nowDate = new Date();
		      nowDate.setMonth(nowDate.getMonth() + 6);
		      cookieExpires = nowDate.toGMTString();
		   }
 
		   if (cookiePath != "")
		   {
		      cookiePath = ";Path=" + cookiePath;
		   }
 
		   document.cookie = cookieName + "=" + cookieValue +
		      ";expires=" + cookieExpires + cookiePath;
		}
 
	function addCookie()
	{
		cookieName = ("cookie" + cookieCounter);
		cookieValue = document.form.item.value;
 
		setCookie(cookieName, cookieValue, "", "");
 
		var printOut = document.getElementById("printout");
		printOut.innerHTML = "Cookie \
							  <font color=009900>" + cookieName + " </font> \
							  has stored the value of \
							  <font color=009900>" + cookieValue + "</font>";
 
		cookieCounter++;
	}
 
 
	function searchCookies()
	{
		var cookieArray = document.cookie.split(";");
		resultsArray = new Array();
 
		var printOut = document.getElementById("printout");
 
		var cookieValue = document.form.item.value;
 
		for (index in cookieArray)
		{
			equalsIndex = cookieArray[index].indexOf("=");
 
			var match = "no";
 
 
			if(cookieValue == "")
			{
				match = "yes";
			}
 
			else
			{
 
				if(cookieArray[index].lastIndexOf(cookieValue) > equalsIndex)
				{
					match = "yes";
				}
			}
 
 
			if (match == "yes")
			{
				resultsArray.push(cookieArray[index] + "<br>");
			}
		 }
 
			printOut.innerHTML = resultsArray;
 
	}
 
 
	function resetEntries()
	{
		var printOut = document.getElementById("printout");
		printOut.innerHTML = "";
	}
 
 
 
</script>
 
 
<body bgcolor="#c0c0c0">
<h1>Assignment 7 - Cookies</h1>
<h2>Search/Add Items</h2>
 
<form name="form">
 
Item Name
<input type="text" name="item" id="item">
<div id="error"></div>
 
<br>
 
<input type="button" value="Search" onclick = searchCookies()>
<input type="button" value="Add" onclick = addCookie()>
<input type="reset" value="Reset"  onclick = resetEntries()>
</form>
 
<br>
 
<div id="printout"></div>
 
</body>
</html>

Open in new window

johnjmkAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
cy-wongConnect With a Mentor Commented:
How about this one?
<html>
<head>
        <title>Cookies</title>
</head>
 
<script language="JavaScript">
 
        var cookieCounter = 1
 
        function getCookieValue(cookieName)
        {
                   var cookieValue = document.cookie;
                   var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
 
                   if (cookieStartsAt == -1)
                   {
                      cookieStartsAt = cookieValue.indexOf(cookieName + "=");
                   }
 
                   if (cookieStartsAt == -1)
                   {
                      cookieValue = null;
                   }
                   else
                   {
                      cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
                      var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
                      if (cookieEndsAt == -1)
                      {
                         cookieEndsAt = cookieValue.length;
                      }
                      cookieValue = unescape(cookieValue.substring(cookieStartsAt,
                         cookieEndsAt));
                   }
 
                   return cookieValue;
                }
 
 
        function setCookie(cookieName, cookieValue, cookiePath, cookieExpires)
        {
                   cookieValue = escape(cookieValue);
 
                   if (cookieExpires == "")
                   {
                      var nowDate = new Date();
                      nowDate.setMonth(nowDate.getMonth() + 6);
                      cookieExpires = nowDate.toGMTString();
                   }
 
                   if (cookiePath != "")
                   {
                      cookiePath = ";Path=" + cookiePath;
                   }
 
                   document.cookie = cookieName + "=" + cookieValue +
                      ";expires=" + cookieExpires + cookiePath;
                }
 
        function addCookie()
        {
                cookieName = ("cookie" + cookieCounter);
                cookieValue = document.form.item.value;
 
                setCookie(cookieName, cookieValue, "", "");
 
                var printOut = document.getElementById("printout");
                printOut.innerHTML = "Cookie \
                                                          <font color=009900>" + cookieName + " </font> \
                                                          has stored the value of \
                                                          <font color=009900>" + cookieValue + "</font>";
 
                cookieCounter++;
        }
 
 
        function searchCookies()
        {
                var cookieArray = document.cookie.split(";");
                var resultsStr = "";
 
                var printOut = document.getElementById("printout");
 
                var cookieValue = document.form.item.value;
 
                for (index in cookieArray)
                {
                        equalsIndex = cookieArray[index].indexOf("=");
 
                        var match = "no";
 
 
                        if(cookieValue == "")
                        {
                                match = "yes";
                        }
 
                        else
                        {
                                if(cookieArray[index].lastIndexOf(cookieValue) > equalsIndex)
                                {
                                        match = "yes";
                                }
                        }
 
 
                        if (match == "yes")
                        {
                                resultsStr += cookieArray[index].substr(equalsIndex+1) + "<br>";
                        }
                 }
 
                 printOut.innerHTML = resultsStr;
 
        }
 
 
        function resetEntries()
        {
                var printOut = document.getElementById("printout");
                printOut.innerHTML = "";
        }
 
 
 
</script>
 
 
<body bgcolor="#c0c0c0">
<h1>Assignment 7 - Cookies</h1>
<h2>Search/Add Items</h2>
 
<form name="form">
 
Item Name
<input type="text" name="item" id="item">
<div id="error"></div>
 
<br>
 
<input type="button" value="Search" onclick = searchCookies()>
<input type="button" value="Add" onclick = addCookie()>
<input type="reset" value="Reset"  onclick = resetEntries()>
</form>
 
<br>
 
<div id="printout"></div>
 
</body>
</html>

Open in new window

0
 
cy-wongCommented:
Hi,
Please see if you like this code.
Thank you
<html>
<head>
        <title>Cookies</title>
</head>
 
<script language="JavaScript">
 
        var cookieCounter = 1
 
        function getCookieValue(cookieName)
        {
                   var cookieValue = document.cookie;
                   var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
 
                   if (cookieStartsAt == -1)
                   {
                      cookieStartsAt = cookieValue.indexOf(cookieName + "=");
                   }
 
                   if (cookieStartsAt == -1)
                   {
                      cookieValue = null;
                   }
                   else
                   {
                      cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
                      var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
                      if (cookieEndsAt == -1)
                      {
                         cookieEndsAt = cookieValue.length;
                      }
                      cookieValue = unescape(cookieValue.substring(cookieStartsAt,
                         cookieEndsAt));
                   }
 
                   return cookieValue;
                }
 
 
        function setCookie(cookieName, cookieValue, cookiePath, cookieExpires)
        {
                   cookieValue = escape(cookieValue);
 
                   if (cookieExpires == "")
                   {
                      var nowDate = new Date();
                      nowDate.setMonth(nowDate.getMonth() + 6);
                      cookieExpires = nowDate.toGMTString();
                   }
 
                   if (cookiePath != "")
                   {
                      cookiePath = ";Path=" + cookiePath;
                   }
 
                   document.cookie = cookieName + "=" + cookieValue +
                      ";expires=" + cookieExpires + cookiePath;
                }
 
        function addCookie()
        {
                cookieName = ("cookie" + cookieCounter);
                cookieValue = document.form.item.value;
 
                setCookie(cookieName, cookieValue, "", "");
 
                var printOut = document.getElementById("printout");
                printOut.innerHTML = "Cookie \
                                                          <font color=009900>" + cookieName + " </font> \
                                                          has stored the value of \
                                                          <font color=009900>" + cookieValue + "</font>";
 
                cookieCounter++;
        }
 
 
        function searchCookies()
        {
                var cookieArray = document.cookie.split(";");
                var resultsStr = "";
 
                var printOut = document.getElementById("printout");
 
                var cookieValue = document.form.item.value;
 
                for (index in cookieArray)
                {
                        equalsIndex = cookieArray[index].indexOf("=");
 
                        var match = "no";
 
 
                        if(cookieValue == "")
                        {
                                match = "yes";
                        }
 
                        else
                        {
 
                                if(cookieArray[index].lastIndexOf(cookieValue) > equalsIndex)
                                {
                                        match = "yes";
                                }
                        }
 
 
                        if (match == "yes")
                        {
                                resultsStr += cookieArray[index] + "<br>";
                        }
                 }
 
                 printOut.innerHTML = resultsStr;
 
        }
 
 
        function resetEntries()
        {
                var printOut = document.getElementById("printout");
                printOut.innerHTML = "";
        }
 
 
 
</script>
 
 
<body bgcolor="#c0c0c0">
<h1>Assignment 7 - Cookies</h1>
<h2>Search/Add Items</h2>
 
<form name="form">
 
Item Name
<input type="text" name="item" id="item">
<div id="error"></div>
 
<br>
 
<input type="button" value="Search" onclick = searchCookies()>
<input type="button" value="Add" onclick = addCookie()>
<input type="reset" value="Reset"  onclick = resetEntries()>
</form>
 
<br>
 
<div id="printout"></div>
 
</body>
</html>

Open in new window

0
 
johnjmkAuthor Commented:
Thank you for quick response! That's better but not exactly what I'm looking for. I want it to print out just the value of the cookie and not the cookie name. For example if a user put in "Stuff" and "Junk" it should list those two words instead of "cookie1=Stuff" and "cookie2=Junk" when a search is done. I hope I'm being clear.
0
 
johnjmkAuthor Commented:
Thanks! That works perfectly!
0
 
johnjmkAuthor Commented:
Thanks! That's exactly what I was looking for.
0
All Courses

From novice to tech pro — start learning today.