3

I found this code in w3schoool JavaScript cookie section, which is to read the cookie:

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 "";
}

In this line:

if (document.cookie.length > 0)

what document.cookie.length means?

In this line:

c_start = document.cookie.indexOf(c_name + "=");

why I need to add = after the c_name(cookie name)?

In this line:

c_start = c_start + c_name.length+1;

why I need to add c_name.length+1? What the purpose?

And what the meaning of this line:

  if (c_end==-1) c_end = document.cookie.length;

Can Anyone answer my question? Thanks!!!

YakovL
  • 7,557
  • 12
  • 62
  • 102
dramasea
  • 3,370
  • 16
  • 49
  • 77
  • 3
    At least one of these questions was already answered in your [previous question](http://stackoverflow.com/questions/4703128/what-mean-length-in-document-cookie). And anyway, why don't you examine the code yourself a bit? You can insert `alert` or `console.log` and see what the content of the variables is and what the differences is between using `+1` or not.... – Felix Kling Jan 16 '11 at 12:41
  • 9
    [W3Schools is a bad reference.](http://w3fools.com/) – Gumbo Jan 16 '11 at 12:42
  • Wish there was a wiki with all references of all libraries and all languages. Not thousands of HTML references, thousands of C references, and so on. :) –  Jan 16 '11 at 12:49
  • 1
    If you want a *good* cookie script, with *full* explanation, see: http://www.quirksmode.org/js/cookies.html – Yi Jiang Jan 16 '11 at 12:50
  • See also [Javascript getCookie functions](http://stackoverflow.com/questions/4003823/javascript-getcookie-functions). – Gumbo Jan 16 '11 at 12:50

4 Answers4

4

document.cookie returns a string containing the cookies. Everything else you ask about is pretty standard javascript string manipulation.

if (document.cookie.length > 0)

checks if the string is not empty.

c_start = document.cookie.indexOf(c_name + "=");

finds the index of the first occurrence of the COOKIENAME= substring in the string.

c_start = c_start + c_name.length + 1;

positions the index after the cookie name in the string

c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) c_end = document.cookie.length;

tries to find the first occurrence of the ; character starting from the c_start position and if this character is not found it positions to the end of the string.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3

see: https://developer.mozilla.org/en/DOM/document.cookie

document.cookie is a string, with key=value pairs separated by semicolons (;).

the code you pasted looks for a specific key in the string, and then finding its value by looking for the end of the string, or the next semicolon, and returning the value it found

so for example, if document.cookie === "someKey=aCookieMadeMeHaveValue7;anotherKey=aShorterValue", you can search for the value of someKey by executing the function getCookie('someKey'), which will look at the string, and return 'aCookieMadeMeHaveValue7'.

it will add +1 to that position so as to jump over the '=', and then return the string from there until the first time it sees a ';' or comes to the end of the string.

davin
  • 44,863
  • 9
  • 78
  • 78
0

OK, quick answers. Firstly, document.cookie is a string containing key=value pairs for each cookie set on this domain.

(1) if (document.cookie.length>0) checks that there are some cookies set, i.e. that the string is not empty.

(2) c_start=document.cookie.indexOf(c_name + "="); the = is needed to make sure that c_name does not occur inside the value of a cookie, only in the key.

(3) c_start=c_start + c_name.length+1; c_start is the place where the key has been found in the string. You then need to add the length of the key plus one (for the =) to find the start of the value.

(4) if (c_end==-1) c_end=document.cookie.length; If the cookie is the last one, there will be no terminating ;, so we look for the very end of the string instead.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0

document.cookie is a string and the length property holds the length of the string in characters.

The = is appended to the cookie name because the cookie name could also appear somewhere else in the document.cookie string (like in the cookie value of another cookie). The c_name.length+1 is used because the +1 reflects the = after the cookie name. And indexOf return -1 if the needle could not be found in the haystack; that’s why c_end is compared to -1.

But you shouldn’t use this implementation. Take a look at my answer to Javascript getCookie functions to see why it’s wrong and how a better implementation could look like.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844