0

so I am new to Javascript and I am attempting to get a cookie value to keep track of a test.

I have six questions and when a question is finished I increment "counter" up 1 value, this way if the user loses connection while on question four the counter would be at four. Then when they reconnect I could get that counter number from the cookie and launch from question four, rather than restarting the user at question one.

Here is my cookie, counter being the number that stores the question number.

 document.cookie = "value1=" + one + ";value2" + two + ";value3=" + three + ";value4" + four + ";value5=" + five + ";value6=" + six + ";count=" + counter + ";expires=" + exp.toUTCString(); 

Here is my attempt in trying to figure out how the values are storing, I have tried looking for counter, "count", "count=", and "count=6"

function getCookie(cookieName) { var name = cookieName + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i].trim(); if ((c.indexOf(i)) == (all the options listed above)) { alert("found"); } } return "undefined"; } 

I cannot figure out what is being stored where. Yes the array should be broken up per ";" via the split() in the cookie. And when I call an alert on each variable "i" in the loop I get values such as "counter=6".

I am having trouble in actually specifying some sort of "if" statement to capture JUST the counter variable.

Thank you!

4
  • There are many tutorials on accessing cookies from JS, I'm sure they all show how to get the value of the cookie you want. Commented May 13, 2014 at 21:17
  • @Barmar I have tried quite a few, most of them involve splitting the cookie string, but then I do not understand it from there. Or they are trying to get all kinds of random values when I only need 1 value from the cookies. Commented May 13, 2014 at 21:32
  • Do you expect the answers here to show you anything different? You split it up, then loop through the array looking for the one you want. If you can't understand that, maybe you should install a plugin that automates it for you. Commented May 13, 2014 at 21:34
  • @Barmar Okay, I have added the most recent attempt I have made. I just cannot figure out what is being stored where. Yes the array should be broken up per ";" in the cookie. And when I call an alert on each variable "i" in the loop I get the contents of the entire cookie, such as "counter=6", I am having trouble in actually specifying some sort of "if" statement to capture JUST the counter variable. Commented May 13, 2014 at 21:37

2 Answers 2

1

You're not using indexOf correctly.

function getCookie(cookieName) { var name = cookieName + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i].trim(); if ((c.indexOf(name)) == 0) { alert("found"); return c.substr(name.length); } } alert("not found"); return null; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, yes. I see that c = the ca[i], but the .indexOf(name) is where I get confused. is each array index of ca holding something like "count=" or "value1=" or something else? And I am confused on how indexOf(name) is helping. Sorry for my slow learning.
split creates an array, where each element is a substring of the original cookie string. So ca[i] will be something like count=3 or value1=abc. c.indexOf(name) returns the position of name in c, so if name is count=, it returns 0 if c is count=3, it returns -1 if c is value1=abc.
1

If you convert the cookie to a key-value pair then it can be easier to access cookies that way. You can do this with the following:

 const cookiesArray = document.cookie.split(";").map((cookie) => cookie.trim()); const cookiesHashmap = cookiesArray.reduce((all, cookie) => { const [cookieName, value] = cookie.split("="); return { [cookieName]: value, ...all, }; }, {}); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.