IMPORTANT!This project was moved khổng lồ https://darkedeneurope.com/js-cookie/js-cookie, check the discussion.
Bạn đang xem: How to set and unset cookies using jquery?
New issues should be opened at https://darkedeneurope.com/js-cookie/js-cookie/issues
If you"re viewing this, you"re reading the documentation for the old repository.View documentation for the lathử nghiệm backwards compatible release (1.5.1).
Build Status Matrix
InstallationInclude script after the jQuery library (unless you are packaging scripts somehow else):
Do not include the script directly from darkedeneurope.com (http://raw.darkedeneurope.com/...). The tệp tin is being served as text/plain & as such being blockedin Internet Explorer on Windows 7 for instance (because of the wrong MIME type). Bottom line: darkedeneurope.com is not a CDoanh Nghiệp.
The plugin can also be loaded as AMD or CommonJS module.
UsageCreate session cookie:
Create expiring cookie, 7 days from then:
$.cookie("name", "value", expires: 7 );
Create expiring cookie, valid across entire site:
$.cookie("name", "value", expires: 7, path: "/" );
"value"$.cookie("nothing"); // => undefined">
$.cookie("name"); // => "value"$.cookie("nothing"); // => undefined
Read all available cookies:
"name": "value" ">
$.cookie(); // => "name": "value"
true$.removeCookie("nothing"); // => false// Need to lớn use the same attributes (path, domain) as what the cookie was written with$.cookie("name", "value", path: "/" );// This won"t work!$.removeCookie("name"); // => false// This will work!$.removeCookie("name", path: "/" ); // => true">
// Returns true when cookie was successfully deleted, otherwise false$.removeCookie("name"); // => true$.removeCookie("nothing"); // => false// Need khổng lồ use the same attributes (path, domain) as what the cookie was written with$.cookie("name", "value", path: "/" );// This won"t work!$.removeCookie("name"); // => false// This will work!$.removeCookie("name", path: "/" ); // => true
Note: when deleting a cookie, you must pass the exact same path, tên miền and secure options that were used to lớn set the cookie, unless you"re relying on the mặc định options that is.
rawBy default the cookie value is encoded/decoded when writing/reading, using encodeURIComponent/decodeURIComponent. Bypass this by setting raw khổng lồ true:
$.cookie.raw = true;
jsonTurn on automatic storage of JSON objects passed as the cookie value. Assumes JSON.stringify and JSON.parse:
$.cookie.json = true;
Cookie OptionsCookie attributes can be phối globally by setting properties of the $.cookie.defaults object or individually for each call to $.cookie() by passing a plain object khổng lồ the options argument. Per-Điện thoại tư vấn options override the mặc định options.
Define lifetime of the cookie. Value can be a Number which will be interpreted as days from time of creation or a Date object. If omitted, the cookie becomes a session cookie.
Define the path where the cookie is valid. By mặc định the path of the cookie is the path of the page where the cookie was created (standard browser behavior). If you want to make it available for instance across the entire domain use path: "/". Default: path of page where the cookie was created.
chú ý regarding Internet Explorer:
Due to an obscure bug in the underlying WinINET InternetGetCookie implementation, IE’s document.cookie will not return a cookie if it was set with a path attribute containing a filename.
(From Internet Explorer Cookie Internals (FAQ))
This means one cannot set a path using path: window.location.pathname in case such pathname contains a filename like so: /kiểm tra.html (or at least, such cookie cannot be read correctly).
Define the domain name where the cookie is valid. Default: tên miền of page where the cookie was created.
If true, the cookie transmission requires a secure protocol (https). Default: false.
ConvertersProvide a conversion function as optional last argument for reading, in order khổng lồ change the cookie"s valueto lớn a different representation on the fly.
Example for parsing a value into a number:
$.cookie("foo", "42");$.cookie("foo", Number); // => 42
Dealing with cookies that have sầu been encoded using escape (3rd party cookies):
$.cookie.raw = true;$.cookie("foo", unescape);
No longer maintained, superseded by JS Cookie: