按名称获取cookie [英] Get cookie by name
问题描述
我有一个getter从cookie获取值。
现在我有两个名为 shares =
的cookie,名称为 obligations =
。
我想使这个getter只从得到的义务cookie的值。
我该如何做?因此,'for'将数据拆分为单独的值并将其放入数组。
function getCookie1(){
//我必须在这里添加什么才能看到obligations =cookie?
//因为现在它搜索所有的cookie。
var elements = document.cookie.split('=');
var obligations = elements [1] .split('%');
for(var i = 0; i< obligations.length - 1; i ++){
var tmp = obligations [i] .split('$');
addProduct1(tmp [0],tmp [1],tmp [2],tmp [3]);
}
}
方法,避免迭代数组,将是:
function getCookie(name){
var value = ;+ document.cookie;
var parts = value.split(;+ name +=);
if(parts.length == 2)return parts.pop()。split(;)。
}
演练
如果token在一个字符串中不存在,或者是一个有两个字符串的数组,那么在token中找到一个字符串(相同值)的数组时,字符串。
第一个(左侧)元素是标记之前的字符串,第二个(右侧)是标记之后的字符串。 p>
(注意:如果字符串以令牌开头,第一个元素是空字符串)
存储如下:
{name} = {value}; {name} = {value}; ...
为了检索特定的cookie值,我们只需要得到; {name} =和下一个;之前。在我们进行任何处理之前,我们在Cookie字符串前加上;,以便每个Cookie名称(包括第一个)都用;和=括起来:
; {name} = {value}; {name} = {value}; ...
现在,我们可以先用; {name} =分割,如果在cookie字符串中找到token(即我们有两个元素)第二个元素是以我们的cookie值开头的字符串。然后我们从数组(即pop)中拉出,并重复相同的过程,但现在使用;作为令牌,但这次拉出左字符串(即shift)以获取实际令牌值。
I have a getter to get the value from a cookie.
Now I have 2 cookies by the name shares=
and by the name obligations=
.
I want to make this getter only to get the values from the obligations cookie.
How do I do this? So the 'for' splits the data into separate values and puts it in an array.
function getCookie1() {
// What do I have to add here to look only in the "obligations=" cookie?
// Because now it searches all the cookies.
var elements = document.cookie.split('=');
var obligations= elements[1].split('%');
for (var i = 0; i < obligations.length - 1; i++) {
var tmp = obligations[i].split('$');
addProduct1(tmp[0], tmp[1], tmp[2], tmp[3]);
}
}
One approach, which avoids iterating over an array, would be:
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
Walkthrough
Splitting a string by token will produce either, an array with one string (same value), in case token does not exist in a string, or an array with two strings , in case token is found in a string .
The first (left) element is string of what was before the token, and the second one (right) is what is string of what was after the token.
(NOTE: in case string starts with a token, first element is an empty string)
Considering that cookies are stored as follows:
"{name}={value}; {name}={value}; ..."
in order to retrieve specific cookie value, we just need to get string that is after "; {name}=" and before next ";". Before we do any processing, we prepend the cookies string with "; ", so that every cookie name, including the first one, is enclosed with "; " and "=":
"; {name}={value}; {name}={value}; ..."
Now, we can first split by "; {name}=", and if token is found in a cookie string (i.e. we have two elements), we will end up with second element being a string that begins with our cookie value. Then we pull that out from an array (i.e. pop), and repeat the same process, but now with ";" as a token, but this time pulling out the left string (i.e. shift) to get the actual token value.
这篇关于按名称获取cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!