JQuery. cookie. js usage, jquery. cookie. js
A lightweight cookie plug-in that can read, write, and delete cookies.
Jquery. cookie. js Configuration
First, it contains the library file of jQuery, followed by the library file of jquery. cookie. js.
<Script type = "text/javascript" src = "js/jquery-1.8.1.min.js"> </script>
<Script type = "text/javascript" src = "js/jquery. cookie. js"> </script>
Usage:
Cookie has four different attributes: name, content, domain, and path.
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.
"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 to read one page from another page
The path of the cookie. The cookie Path is used to set the top-level directory that can read cookies. Set this
PATH is set as the root directory of the website, so that all webpages can read cookies from each other (generally do not set this way 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.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.