[英]Clarification about the JavaScript getCookie function?
The getCookie
function is first called in a checkCookie function as so: 首先在checkCookie函数中调用
getCookie
函数,如下所示:
var username=getCookie("username");
And this is the function: 这是函数:
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
I'm lost on what is happening overall.. why do you split on ;
我迷失了整体发生的事情
;
, and mainly what is the reason for the the lines with x
and y
. ,主要是
x
和y
线的原因是什么。 The source is here . 来源在这里 。 I appreciate any tips or advice.
我感谢任何提示或建议。
The most effective way to get a cookie value is to use regular expressions. 获取cookie值的最有效方法是使用正则表达式。
function cookie_get(n){
return (n=(document.cookie+';').match(new RegExp(n+'=.*;')))&&n[0].split(/=|;/)[1]
}
cookies are always stored as: key1=value1;key2=value2 Cookie始终存储为:key1 = value1; key2 = value2
so the split on ; 所以分裂; is to read all the key-value pairs into the ARRcookies variable.
是将所有键值对读入ARRcookies变量。 Then, for each cookie, the key is read into x, and the value into y
然后,对于每个cookie,将密钥读入x,将值读入y
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.