dar*_*ryl 14 javascript cookies jquery
例如,我有这个cookie:
$.cookie("foo", "500", { path: '/', expires: 365 });
Run Code Online (Sandbox Code Playgroud)
如何获取该cookie的值并将其放入变量中?
例如(我知道这不正确):
var foo = $.cookie("foo").val();
Run Code Online (Sandbox Code Playgroud)
这对我有用:
function getCookieValue(cname) {
// cname is the cookie name (foo) which value you are after
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 "";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39682 次 |
| 最近记录: |