如何使用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关闭。
其他回答
你可以在Mozilla网站上使用这个库
你可以像这样设置和获取饼干
docCookies.setItem(name, value);
docCookies.getItem(name);
你可以在这里使用一个插件。
https://plugins.jquery.com/cookie/
然后写一个饼干做 美元。饼干(“测试”,1);
访问设置cookie做 美元.cookie(“测试”);
我知道,已经有很多答案了,但这里有一个设置,获取和删除所有漂亮的香草,并很好地放入全局引用:
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
请注意,饼干持续的时间不长,但从我们的角度来看,基本上是这样的。通过查看这里的字符串或其他答案,您可以很容易地了解如何更改日期。
我认为Fresher给出了一个很好的方法,但是有一个错误
<script type="text/javascript">
function setCookie(key, value) {
var expires = new Date();
expires.setTime(expires.getTime() + (value * 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;
}
</script>
你应该在getTime()附近添加"value";否则cookie将立即过期:)
如何使用它?
//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关闭。
推荐文章
- ReactJS和公共文件夹中的图像
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 如何在JSON中使用杰克逊更改字段名
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- HttpOnly cookie如何与AJAX请求一起工作?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?