Home >Web Front-end >JS Tutorial >How Can I Set, Get, and Delete Cookies Using jQuery (and js-cookie)?

How Can I Set, Get, and Delete Cookies Using jQuery (and js-cookie)?

DDD
DDDOriginal
2024-12-31 13:21:10788browse

How Can I Set, Get, and Delete Cookies Using jQuery (and js-cookie)?

Setting and Unsetting Cookies with jQuery

jQuery offers a convenient way to handle cookies:

Set Cookie

Set a cookie named "test" and set its value to 1:

$.cookie("test", 1);

Delete Cookie e

Delete cookie named "test":

$.removeCookie("test");

Advanced options

Set cookie expiration time (here for 10 days):

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

Cover all options:

$.cookie("test", 1, {
   expires : 10, // 过期时间
   path    : '/', // 路径
   domain  : 'jquery.com', // 域名
   secure  : true // 安全
});

Read Cookie

Read the value of the cookie named "test":

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

Updated (2015 4 month)

js-cookie library provides support for jQ A standalone solution for the uery version with the same functionality:

https://github.com/js-cookie/js-cookie

It operates cookies with a similar syntax to the jQuery version.

The above is the detailed content of How Can I Set, Get, and Delete Cookies Using jQuery (and js-cookie)?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn