[英]How to read a cookie value in Jquery and create another cookie with same value
How do i get value of "id" from a session cookie "apple": Decoded below as 我如何从会话Cookie“苹果”中获取“ id”的值:
"{logo:"Y",id:"5555555555"}"
id ="5555555555"
from apple
apple
获得id ="5555555555"
值 banana
and place this value "id" into it which expires in 10 days. banana
”的持久性cookie,并将该值“ id”放入其中,该ID在10天后过期。 Pasted My code below: 在下面粘贴我的代码:
Var res = $.cookie("apple");
<<Code to split it and get "id">>
$.cookie('id', 'the_value', { expires: 10});
I am new to Jquery and i am trying hard to get the basics . 我是Jquery的新手,我正在努力获取基础知识。 Please help!
请帮忙!
Parse the JSON string in the cookie, then get the id
property from it. 解析cookie中的JSON字符串,然后从中获取
id
属性。 You can then store this in the new cookie. 然后,您可以将其存储在新的Cookie中。
var obj = JSON.parse(res);
$.cookie('banana', obj.id, { expires: 10 });
Check the usage section in the readme here: https://github.com/carhartl/jquery-cookie#usage 在此处查看自述文件中的用法部分: https : //github.com/carhartl/jquery-cookie#usage
Usage
用法
Create session cookie:
创建会话Cookie:
$.cookie('name', 'value');
Create expiring cookie, 7 days from then:
从7天开始创建过期的Cookie:
$.cookie('name', 'value', { expires: 7 });
Create expiring cookie, valid across entire site:
创建在整个网站上有效的过期Cookie:
$.cookie('name', 'value', { expires: 7, path: '/' });
Read cookie:
读取Cookie:
$.cookie('name'); // => "value" $.cookie('nothing'); // => undefined
Read all available cookies:
阅读所有可用的cookie:
$.cookie(); // => { "name": "value" }
Delete cookie:
删除Cookie:
// Returns true when cookie was successfully deleted, otherwise false $.removeCookie('name'); // => true $.removeCookie('nothing'); // => false // Need to use the same attributes (path, domain) as what the cookie was written with $.cookie('name', 'value', { path: '/' }); // This won't work! $.removeCookie('name'); // => false // This will work! $.removeCookie('name', { path: '/' }); // => true
Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie, unless you're relying on the default options that is.
注意:删除Cookie时,除非您依赖默认选项,否则您必须传递用于设置Cookie的完全相同的路径,域和安全选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.