我有一个getter从cookie中获取值。
现在我有了两个cookie,名字分别是shares=和obligation =。
我想让这个getter只从义务cookie中获取值。
我怎么做呢?因此for语句将数据拆分为单独的值,并将其放入数组中。
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]);
}
}
其他一些使用正则表达式的答案中的方法并不涵盖所有情况,特别是:
当饼干是最后一块时。在这种情况下,cookie值后不会有分号。
当另一个cookie名称以正在查找的名称结束时。例如,您正在寻找名为“one”的cookie,而有一个名为“done”的cookie。
cookie名称中包含的字符在正则表达式中使用时不会被解释为字符本身,除非它们前面有反斜杠。
下面的方法可以处理这些情况:
function getCookie(name) {
function escape(s) { return s.replace(/([.*+?\^$(){}|\[\]\/\\])/g, '\\$1'); }
var match = document.cookie.match(RegExp('(?:^|;\\s*)' + escape(name) + '=([^;]*)'));
return match ? match[1] : null;
}
如果没有找到cookie,将返回null。如果cookie值为空,则返回空字符串。
注:
这个函数假设cookie名称是区分大小写的。
文档。cookie——当this出现在赋值的右侧时,它表示一个字符串,其中包含一个以分号分隔的cookie列表,这些cookie又是名称=值对。每个分号后面似乎都有一个空格。
String.prototype.match() -当没有找到匹配时返回null。找到匹配项时返回一个数组,索引[1]处的元素是第一个匹配组的值。
正则表达式
(?:xxxx) -组成不匹配的组。
^ -匹配字符串的开头。
| -为组分离可选模式。
\\s* -匹配一个分号后面跟着零个或多个空格。
= -匹配一个等号。
(xxxx) -组成匹配组。
[^;]* -匹配零个或多个分号以外的字符。这意味着它将匹配最大(但不包括)分号或字符串末尾的字符。
如果你使用jQuery,我建议你使用这个插件:
https://github.com/carhartl/jquery-cookie
https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js
<script type="text/javascript"
src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js">
所以你可以这样读cookie:
var value = $.cookie("obligations");
你也可以写cookie:
$.cookie('obligations', 'new_value');
$.cookie('obligations', 'new_value', { expires: 14, path: '/' });
删除cookie:
$.removeCookie('obligations');
其他一些使用正则表达式的答案中的方法并不涵盖所有情况,特别是:
当饼干是最后一块时。在这种情况下,cookie值后不会有分号。
当另一个cookie名称以正在查找的名称结束时。例如,您正在寻找名为“one”的cookie,而有一个名为“done”的cookie。
cookie名称中包含的字符在正则表达式中使用时不会被解释为字符本身,除非它们前面有反斜杠。
下面的方法可以处理这些情况:
function getCookie(name) {
function escape(s) { return s.replace(/([.*+?\^$(){}|\[\]\/\\])/g, '\\$1'); }
var match = document.cookie.match(RegExp('(?:^|;\\s*)' + escape(name) + '=([^;]*)'));
return match ? match[1] : null;
}
如果没有找到cookie,将返回null。如果cookie值为空,则返回空字符串。
注:
这个函数假设cookie名称是区分大小写的。
文档。cookie——当this出现在赋值的右侧时,它表示一个字符串,其中包含一个以分号分隔的cookie列表,这些cookie又是名称=值对。每个分号后面似乎都有一个空格。
String.prototype.match() -当没有找到匹配时返回null。找到匹配项时返回一个数组,索引[1]处的元素是第一个匹配组的值。
正则表达式
(?:xxxx) -组成不匹配的组。
^ -匹配字符串的开头。
| -为组分离可选模式。
\\s* -匹配一个分号后面跟着零个或多个空格。
= -匹配一个等号。
(xxxx) -组成匹配组。
[^;]* -匹配零个或多个分号以外的字符。这意味着它将匹配最大(但不包括)分号或字符串末尾的字符。
cookie示例:
JS:
document.cookies = {
create : function(key, value, time){
if (time) {
var date = new Date();
date.setTime(date.getTime()+(time*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = key+"="+value+expires+"; path=/";
},
erase : function(key){
this.create(key,"",-1);
},
read : function(key){
var keyX = key + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(keyX) == 0) return c.substring(keyX.length,c.length);
}
return null;
}
}
使用json或xml存储数组和对象
只是为了给这个响应添加一个“正式”的答案,我复制/粘贴解决方案来设置和从MDN检索cookie(这里是JSfiddle
document.cookie = "test1=Hello";
document.cookie = "test2=World";
var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)test2\s*\=\s*([^;]*).*$)|^.*$/, "$1");
function alertCookieValue() {
alert(cookieValue);
}
在您的特定情况下,您将使用以下函数
function getCookieValue() {
return document.cookie.replace(/(?:(?:^|.*;\s*)obligations\s*\=\s*([^;]*).*$)|^.*$/, "$1");
}
注意,我只是用“义务”替换了示例中的“test2”。