jquery java cookie操作_如何使用jQuery设置/取消设置Cookie?

如何使用jQuery设置和取消设置cookie,例如创建一个名为test的cookie并将其值设置为1 ?

#1楼

2019年4月更新

Cookie的读取/操作不需要jQuery,因此请不要使用下面的原始答案。

基本示例:

// Set a cookie

Cookies.set('name', 'value');

// Read the cookie

Cookies.get('name') => // => 'value'

有关详细信息,请参见github上的文档。

参见插件:

然后,您可以执行以下操作:

$.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");

如果cookie是在与当前路径不同的路径上创建的,则您可能希望指定path参数:

var cookieValue = $.cookie("test", { path: '/foo' });

更新(2015年4月):

如下面的评论所述,使用原始插件的团队已删除了新项目( https://github.com/js-cookie/js-cookie )中的jQuery依赖项,该项目具有与相同的功能和通用语法jQuery版本。 显然,原始插件没有任何用。

#2楼

您可以使用此处提供的插件。

然后写一个cookie做$.cookie("test", 1);

要访问设置的cookie,请执行$.cookie("test");

#3楼

无需特别使用jQuery来操作cookie。

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);

}

看一眼

#4楼

在浏览器中设置Cookie的简单示例:

jquery.cookie Test Suite

$(function() {

if ($.cookie('cookieStore')) {

var data=JSON.parse($.cookie("cookieStore"));

$('#name').text(data[0]);

$('#address').text(data[1]);

}

$('#submit').on('click', function(){

var storeData = new Array();

storeData[0] = $('#inputName').val();

storeData[1] = $('#inputAddress').val();

$.cookie("cookieStore", JSON.stringify(storeData));

var data=JSON.parse($.cookie("cookieStore"));

$('#name').text(data[0]);

$('#address').text(data[1]);

});

});

Name

Address



简单只需复制/粘贴并使用此代码来设置您的cookie。

#5楼

function setCookie(key, value, expiry) {

var expires = new Date();

expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));

document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();

}

function getCookie(key) {

var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');

return keyValue ? keyValue[2] : null;

}

function eraseCookie(key) {

var keyValue = getCookie(key);

setCookie(key, keyValue, '-1');

}

您可以像这样设置Cookie

setCookie('test','1','1'); //(key,value,expiry in days)

你可以像这样获得饼干

getCookie('test');

最后,您可以像这样擦除cookie

eraseCookie('test');

希望它将对某人有所帮助:)

编辑:

如果要将cookie设置为所有路径/页面/目录,则将path属性设置为cookie

function setCookie(key, value, expiry) {

var expires = new Date();

expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));

document.cookie = key + '=' + value + ';path=/' + ';expires=' + expires.toUTCString();

}

谢谢,vicky

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值