admin管理员组

文章数量:1416329

Okay so i have started learning javascript from the book Beginning Javascript 5th ed, just confused by a js script

function getCookieValue(name) {
var value = document.cookie;
var cookieStartsAt = value.indexOf(" " + name + "=");
if (cookieStartsAt == -1) {
    cookieStartsAt = value.indexOf(name + "=");
}
if (cookieStartsAt == -1) {
    value = null;
} else {
    cookieStartsAt = value.indexOf("=", cookieStartsAt) + 1;
    var cookieEndsAt = value.indexOf(";", cookieStartsAt);
    if (cookieEndsAt == -1) {
        cookieEndsAt = value.length;
    }
    value = unescape(value.substring(cookieStartsAt,
       cookieEndsAt));
}
return value;}

My question is how does the indexOf operator works here( i know how it works and used it previously) ?? The above program is defined below by the book which goes as :

The first task of the function is to get the document.cookie string and store it in the value variable

var value = document.cookie;

Next, you need to find out where the cookie with the name passed as a parameter to the function is within the value string. You use the inde x Of() method of the String object to find this information, as shown in the following line:

var cookieStartsAt = value.indexOf(" " + name + "=");

The method will return either the character position where the individual cookie is found or ‐1 if no such name, and therefore no such cookie, exists. You search on " " + name + "=" so that you don’inadvertently find cookie names or values containing the name that you require. For example, if you have xFoo, Foo, and yFoo as cookie names, a search for Foo without a space in front would match xFoo first, which is not what you want!

What the just just happened here?? How did they achieve the location of the name using indexOf() ?? please explain ? I couldn't understand the xfoo,foo,yfoo example ?? Looking for a simpler example.

Okay so i have started learning javascript from the book Beginning Javascript 5th ed, just confused by a js script

function getCookieValue(name) {
var value = document.cookie;
var cookieStartsAt = value.indexOf(" " + name + "=");
if (cookieStartsAt == -1) {
    cookieStartsAt = value.indexOf(name + "=");
}
if (cookieStartsAt == -1) {
    value = null;
} else {
    cookieStartsAt = value.indexOf("=", cookieStartsAt) + 1;
    var cookieEndsAt = value.indexOf(";", cookieStartsAt);
    if (cookieEndsAt == -1) {
        cookieEndsAt = value.length;
    }
    value = unescape(value.substring(cookieStartsAt,
       cookieEndsAt));
}
return value;}

My question is how does the indexOf operator works here( i know how it works and used it previously) ?? The above program is defined below by the book which goes as :

The first task of the function is to get the document.cookie string and store it in the value variable

var value = document.cookie;

Next, you need to find out where the cookie with the name passed as a parameter to the function is within the value string. You use the inde x Of() method of the String object to find this information, as shown in the following line:

var cookieStartsAt = value.indexOf(" " + name + "=");

The method will return either the character position where the individual cookie is found or ‐1 if no such name, and therefore no such cookie, exists. You search on " " + name + "=" so that you don’inadvertently find cookie names or values containing the name that you require. For example, if you have xFoo, Foo, and yFoo as cookie names, a search for Foo without a space in front would match xFoo first, which is not what you want!

What the just just happened here?? How did they achieve the location of the name using indexOf() ?? please explain ? I couldn't understand the xfoo,foo,yfoo example ?? Looking for a simpler example.

Share Improve this question edited Feb 29, 2016 at 18:19 GolezTrol 116k18 gold badges185 silver badges214 bronze badges asked Feb 29, 2016 at 18:17 Syndicate Syndicate 331 silver badge3 bronze badges 1
  • 1 Looks like a poor tutorial. Maybe I'm wrong, but I think their way of reading a cookie is not secure and doesn't make sense. First of all, cookies are separated by semi-colons, but also, like you say, you could find a cookie containing cookiename= in its value. I would use var cookies = value.split(';');. After that, you got an array with the separate cookies as items, each in the name=value notation. Their method is (trying to be) more efficient, but IMO needlessly plex and not water proof. – GolezTrol Commented Feb 29, 2016 at 18:22
Add a ment  | 

2 Answers 2

Reset to default 3

document.cookie contains a string like cookiename=cookievalue

indexOf is getting the position of the begining of the value part of the cookie

var cookieStartsAt = value.indexOf("cookiename=");

That allows you to use that number to get the value portion of the string with substring()

Useful function indexOf from position

function IndexAt(base,t /*string*/,pos=0) /*int*/ {
 if (pos<0) {pos=0;}
 var x=base.substr(pos);
 var p=x.indexOf(t);
 if (p>=0) { return p+pos;} else {return -1;}
}

本文标签: cookiesindexOf() in javascriptStack Overflow