栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用jQuery设置/取消设置Cookie?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用jQuery设置/取消设置Cookie?

2019年4月更新

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

转到https://github.com/js-cookie/js-cookie,然后在其中使用不依赖jQuery的库。

基本示例:

// Set a cookiecookies.set('name', 'value');// Read the cookiecookies.get('name') => // => 'value'

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


参见插件:

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

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

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

如下面的评论所述,使用原始插件的团队已在新项目中删除了jQuery依赖项,该项目具有与相同的功能和通用语法jQuery版本。显然,原始插件并没有到任何地方。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/383186.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号