如何使用jQuery设置和取消设置cookie,例如创建一个名为test的cookie并将值设置为1?
当前回答
如何使用它?
//To set a cookie
$.cookie('the_cookie', 'the_value');
//Create expiring cookie, 7 days from then:
$.cookie('the_cookie', 'the_value', { expires: 7 });
//Create expiring cookie, valid across entire page:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
//Read cookie
$.cookie('the_cookie'); // => 'the_value'
$.cookie('not_existing'); // => null
//Delete cookie by passing null as value:
$.cookie('the_cookie', null);
// Creating cookie with all availabl options
$.cookie('myCookie2', 'myValue2', { expires: 7, path: '/', domain: 'example.com',
secure: true, raw: true });
可用的选项:
expires:定义cookie的生存时间。Value可以是一个Number(将被解释为从创建时间到日期的天数)或一个Date对象。如果省略,则该cookie为会话cookie。
path:定义cookie有效的路径。默认情况下,cookie的路径是创建cookie的页面的路径(标准的浏览器行为)。如果你想让它在整个页面都可用,使用路径:'/'。
domain:创建cookie的页面域。
secure:默认值:false。如果为真,则cookie传输需要安全协议(https)。
raw:默认情况下,cookie在创建/读取时使用encodeURIComponent/ decodeURIComponent进行编码/解码。通过设置raw: true关闭。
其他回答
Try(这里的doc, SO代码段不工作,所以运行这个)
document.cookie = "test=1" // set
document.cookie = "test=1;max-age=0" // unset
你可以在这里使用一个插件。
https://plugins.jquery.com/cookie/
然后写一个饼干做 美元。饼干(“测试”,1);
访问设置cookie做 美元.cookie(“测试”);
不需要特别使用jQuery来操作cookie。
来自QuirksMode(包括转义字符)
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = encodeURIComponent(name) + "=";
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(nameEQ) === 0)
return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
看一看
我如何删除一个现有的类名,并添加一个新的与jQuery和cookies?
2019年4月更新
读取/操作cookie不需要jQuery,所以不要使用下面的原始答案。
请访问https://github.com/js-cookie/js-cookie,使用那里不依赖jQuery的库。
基本的例子:
// Set a cookie
Cookies.set('name', 'value');
// Read the cookie
Cookies.get('name') => // => 'value'
详见github上的文档。
2019年4月前(老)
查看插件:
https://github.com/carhartl/jquery-cookie
你可以这样做:
$.cookie("test", 1);
删除:
$.removeCookie("test");
此外,要在cookie上设置一个特定天数的超时(这里是10天):
$.cookie("test", 1, { expires : 10 });
如果省略expires选项,则cookie将成为会话cookie,并在浏览器退出时删除。
要涵盖所有选项:
$.cookie("test", 1, {
expires : 10, // Expires in 10 days
path : '/', // The value of the path attribute of the cookie
// (Default: path of page that created the cookie).
domain : 'jquery.com', // The value of the domain attribute of the cookie
// (Default: domain of page that created the cookie).
secure : true // If set to true the secure attribute of the cookie
// will be set and the cookie transmission will
// require a secure protocol (defaults to false).
});
读取cookie的值。
var cookieValue = $.cookie("test");
更新(2015年4月):
正如下面的评论所述,开发原始插件的团队已经在一个新项目(https://github.com/js-cookie/js-cookie)中删除了jQuery依赖项,该项目具有与jQuery版本相同的功能和通用语法。显然,原来的插件并没有去任何地方。
我知道,已经有很多答案了,但这里有一个设置,获取和删除所有漂亮的香草,并很好地放入全局引用:
window.cookieMonster = window.cookieMonster ||
{
// https://stackoverflow.com/a/25490531/1028230
get: function (cookieName) {
var b = document.cookie.match('(^|;)\\s*' + cookieName + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
},
delete: function (name) {
document.cookie = '{0}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
.replace('{0}', name);
},
set: function (name, value) {
document.cookie =
'{0}={1};expires=Fri, 31 Dec 9999 23:59:59 GMT;path=/;SameSite=Lax'
.replace('{0}', name)
.replace('{1}', value);
}
};
请注意,cookie getting regex是从另一个城堡的一个问题的答案中截取的。
我们来测试一下:
cookieMonster.set('chocolate', 'yes please');
cookieMonster.set('sugar', 'that too');
console.log(cookieMonster.get('chocolate'));
console.log(document.cookie);
cookieMonster.delete('chocolate');
console.log(cookieMonster.get('chocolate'));
console.log(document.cookie);
如果你在尝试之前没有任何饼干,应该给你…
yes please
chocolate=yes please; sugar=that too
sugar=that too
请注意,饼干持续的时间不长,但从我们的角度来看,基本上是这样的。通过查看这里的字符串或其他答案,您可以很容易地了解如何更改日期。
推荐文章
- 使用jQuery以像素为整数填充或边距值
- 检查是否选择了jQuery选项,如果没有选择默认值
- Next.js React应用中没有定义Window
- 如何重置笑话模拟函数调用计数之前,每次测试
- 如何强制一个功能React组件渲染?
- 在javascript中从平面数组构建树数组
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 如何在AngularJS中观察路由变化?
- JavaScript DOM删除元素
- 将dd-mm-yyyy字符串转换为日期
- Javascript复选框onChange
- Javascript函数前导bang !语法
- 如何在页面上遍历所有DOM元素?
- 使用jQuery滚动到一个div
- 在JS/jQuery中触发按键/按键/按键事件?