cookies - indexOf() in javascript - Stack Overflow

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745249237a4618577.html

相关推荐

  • cookies - indexOf() in javascript - Stack Overflow

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

    3小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信