javascript 任何人都可以向我解释 document.cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4705381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Anyone can explain to me document.cookie
提问by dramasea
I found this code in w3schoool JavaScript cookie section, which is to read the cookie:
我在w3schoool JavaScript cookie 部分找到了这段代码,就是读取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.lengthmeans?
什么document.cookie.length意思?
In this line:
在这一行:
c_start = document.cookie.indexOf(c_name + "=");
why I need to add =after the c_name(cookie name)?
为什么我需要在=之后添加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?
为什么我需要添加c_name.length+1?什么目的?
And what the meaning of this line:
这行的含义是什么:
if (c_end==-1) c_end = document.cookie.length;
Can Anyone answer my question? Thanks!!!
有人可以回答我的问题吗?谢谢!!!
采纳答案by davin
see: https://developer.mozilla.org/en/DOM/document.cookie
请参阅:https: //developer.mozilla.org/en/DOM/document.cookie
document.cookie is a string, with key=value pairs separated by semicolons (;).
document.cookie 是一个字符串,键=值对由分号 (;) 分隔。
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'.
例如,如果 document.cookie === "someKey=aCookieMadeMeHaveValue7;anotherKey=aShorterValue",则可以通过执行函数 getCookie('someKey') 来搜索 someKey 的值,该函数会查看字符串,并返回 ' 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.
它会将 +1 添加到该位置以便跳过“=”,然后从那里返回字符串,直到它第一次看到“;” 或到达字符串的末尾。
回答by Darin Dimitrov
document.cookiereturns a string containing the cookies. Everything else you ask about is pretty standard javascript string manipulation.
document.cookie返回一个包含 cookie 的字符串。你问的其他一切都是非常标准的 javascript 字符串操作。
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.
查找COOKIENAME=子字符串在字符串中第一次出现的索引。
c_start = c_start + c_name.length + 1;
positions the index after the cookie name in the string
将索引定位在字符串中 cookie 名称之后
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_startposition and if this character is not found it positions to the end of the string.
尝试;从该c_start位置开始找到该字符的第一次出现,如果未找到该字符,则将其定位到字符串的末尾。
回答by lonesomeday
OK, quick answers. Firstly, document.cookieis a string containing key=valuepairs for each cookie set on this domain.
好的,快速回答。首先,document.cookie是一个字符串,其中包含key=value此域上设置的每个 cookie 的对。
(1) if (document.cookie.length>0)checks that there are some cookies set, i.e. that the string is not empty.
(1)if (document.cookie.length>0)检查是否设置了一些 cookie,即字符串不为空。
(2) c_start=document.cookie.indexOf(c_name + "=");the =is needed to make sure that c_namedoes not occur inside the value of a cookie, only in the key.
(2)c_start=document.cookie.indexOf(c_name + "=");的=需要,以确保c_name没有一个cookie的值内发生,只有在关键的。
(3) c_start=c_start + c_name.length+1;c_startis 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.
(3)c_start=c_start + c_name.length+1;c_start是在字符串中找到密钥的地方。然后,您需要将键的长度加一(对于=)以找到值的开头。
(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.
(4)if (c_end==-1) c_end=document.cookie.length;如果 cookie 是最后一个,则不会有 termination ;,因此我们查找字符串的最末尾。
回答by Gumbo
document.cookieis a string and the lengthproperty holds the length of the string in characters.
document.cookie是一个字符串,该length属性以字符为单位保存字符串的长度。
The =is appended to the cookie name because the cookie name could also appear somewhere else in the document.cookiestring (like in the cookie value of another cookie). The c_name.length+1is used because the +1reflects the =after the cookie name. And indexOfreturn -1if the needle could not be found in the haystack; that's why c_endis compared to -1.
该=附加到cookie的名字,因为cookie的名称也可能出现在其他地方的document.cookie字符串(如在另一个cookie的cookie值)。c_name.length+1之所以使用,是因为+1反映=了 cookie 名称之后。如果在大海捞针中找不到针,则indexOf返回-1;这就是为什么c_end与-1.
But you shouldn't use this implementation. Take a look at my answer to Javascript getCookie functionsto see why it's wrong and how a better implementation could look like.
但是你不应该使用这个实现。看看我对Javascript getCookie 函数的回答,看看为什么它是错误的,以及更好的实现会是什么样子。

