[英]Retrieve jQuery Cookie value
例如,我有这个 cookie:
$.cookie("foo", "500", { path: '/', expires: 365 });
如何获取该 cookie 的值并将其放入变量中?
例如(我知道这是不正确的):
var foo = $.cookie("foo").val();
这只是var foo = $.cookie("foo")
。
不需要.val()
调用,因为您没有访问 DOM 元素的值。
通过这种方式我们可以访问
console.log($.cookie());
//它将以object的形式给出所有cookies
alert($.cookie('foo'));
//它将给cookie foo值即500
要获取 cookie 的值,您只需调用它的引用即可。 例如:
$.cookie("foo", "somevalue");
alert($.cookie("foo"));
会提醒:
somevalue
function getCookieValue(cname) { // cname is nothing but the cookie value which
//contains the value
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.