Jquery. cookie. js is a lightweight cookie plug-in that can read, write, and delete cookies.
First, it contains the library file of jQuery, followed by the library file of jquery. cookie. js.
<script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/jquery.cookie.js"></script>
Usage
1. Add a new session cookie:
$.cookie('the_cookie', 'the_value');
Note: When the cookie validity period is not specified, the created cookie is valid until the user closes the browser by default. Therefore, it is called "session cookie )".
2. Create a cookie and set the validity period to 7 days:
$.cookie('the_cookie', 'the_value', { expires: 7 });
Note: When the cookie validity period is specified, the created cookie is called "persistent cookie )".
3. Create a cookie and set the valid cookie Path:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
Note: by default, only webpages with cookie settings can read the cookie. If you want a page to read the cookie set for another page, you must set the cookie Path. The cookie Path is used to set the top-level directory that can read cookies. Set this path as the root directory of the website so that all webpages can read cookies from each other (do not set it like this to prevent conflicts ).
4. Read cookie:
$. Cookie (the _ cookie); // cookie existence => 'the _ value' $. cookie ('not _ existing'); // cookie does not exist => null
5. Delete the cookie and pass null as the cookie value:
$.cookie('the_cookie', null);
---------- Description of relevant parameters ---------------
1). expires: 365
Defines the cookie validity period. The value can be a number (counted from the cookie creation time, in days) or a Date pair.
Image. If this parameter is omitted, the created cookie is a session cookie and will be deleted when the user exits the browser.
2). path :'/'
Default: Only webpages with cookie settings can read the cookie.
Defines the valid cookie Path. By default, the value of this parameter is the path of the page where the cookie is created (the behavior of the standard browser ).
If you want to access this cookie on the entire website, you need to set the valid path as follows: path :'/'. If you want to delete a definition
Cookie with a valid path. You need to include this path when calling the function: $. cookie (the _ cookie, null,
{Path :'/'});. Domain: 'example. com'
Default Value: The domain name of the webpage for which the cookie is created.
3). secure: true
Default Value: false. If the value is true, the secure protocol (HTTPS) is required for cookie transmission ).
4). raw: true
Default Value: false.
By default, the system automatically performs encoding and decoding when reading and writing cookies (using encodeURIComponent encoding,
DecodeURIComponent decoding ). To disable this function, set raw: true.
Instance summary:
// Save the COOKIE: $. cookie ("products", cookieContent, {expires: 1}); // Delete the COOKIE: $. cookie ("products", null );
This is not the case. You can add or delete an object. You can change it to this type!
// Save the COOKIE: $. cookie ("products", cookieContent, {expires: 1, path: '/'}); // Delete COOKIE: $. cookie ("products", null, {path :'/'});
The difference is that you need to assign the path, or you cannot delete it!
The original article is taken from the blog site jquery. cookie usage