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
Release: 2024-12-31 13:21:10
Original
742 people have browsed it

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);
Copy after login

Delete Cookie e

Delete cookie named "test":

$.removeCookie("test");
Copy after login

Advanced options

Set cookie expiration time (here for 10 days):

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

Cover all options:

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

Read Cookie

Read the value of the cookie named "test":

var cookieValue = $.cookie("test");
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template