Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

javascript - 如何使用jQuery设置/取消设置Cookie?(How do I set/unset a cookie with jQuery?)

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

  ask by omg translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update April 2019

(2019年4月更新)

jQuery isn't needed for cookie reading/manipulation, so don't use the original answer below.

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

Go to https://github.com/js-cookie/js-cookie instead, and use the library there that doesn't depend on jQuery.

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

Basic examples:

(基本示例:)

// Set a cookie
Cookies.set('name', 'value');

// Read the cookie
Cookies.get('name') => // => 'value'

See the docs on github for details.

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


See the plugin:

(参见插件:)

https://github.com/carhartl/jquery-cookie

(https://github.com/carhartl/jquery-cookie)

You can then do:

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

$.cookie("test", 1);

To delete:

(删除:)

$.removeCookie("test");

Additionally, to set a timeout of a certain number of days (10 here) on the cookie:

(此外,要在Cookie上设置特定天数(此处为10天)的超时时间:)

$.cookie("test", 1, { expires : 10 });

If the expires option is omitted, then the cookie becomes a session cookie and is deleted when the browser exits.

(如果省略expires选项,则cookie成为会话cookie,并在浏览器退出时被删除。)

To cover all the options:

(涵盖所有选项:)

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

To read back the value of the cookie:

(读取cookie的值:)

var cookieValue = $.cookie("test");

You may wish to specify the path parameter if the cookie was created on a different path to the current one:

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

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

UPDATE (April 2015):

(更新(2015年4月):)

As stated in the comments below, the team that worked on the original plugin has removed the jQuery dependency in a new project ( https://github.com/js-cookie/js-cookie ) which has the same functionality and general syntax as the jQuery version.

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

Apparently the original plugin isn't going anywhere though.

(显然,原始插件没有任何用。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...