Jquery. cookie. js is a jquery-based plug-in, a lightweight cookie plug-in that can read, write, and delete cookies. Next, I will introduce you to the Jquery operation cookie to remember the user name. For more information, see.
1. Introduction to jquery. cookie. js
Jquery. cookie. js is a jquery-based plug-in, a lightweight cookie plug-in that can read, write, and delete cookies.
Jquery. cookie. js can get source code https://github.com/carhartl/jquery-cookie from Github
II. Introduction to basic usage of jquery. cookie. js
JQuery uses the cookie plugin as follows:
1. Read the Cookie value
$. Cookie (the _ cookie); // if it exists, cookieValue is returned; otherwise, null is returned.
2. Set the cookie value
(1) default settings. When no cookie time is specified, the created cookie is valid until the user's browser is disabled by default. Therefore, it is called a session cookie.
$. Cookie (the _ cookie, the _ value ');
(2) Set a cookie with time. When the time is specified, it is called a persistent cookie and the validity period is day.
$. Cookie ('cookiename', 'cookievalue ', {expires: 7 });
(3) set a cookie with a path. If you do not set a valid path, you can only read the cookie on the current page of the cookie setting by default. The cookie Path is used to set the top-level directory that can read the cookie.
$. Cookie ('cookiename', 'cookievalue ', {expires: 7, path :'/'});
(4) set cookies for a specific website.
$. Cookie ('cookiename', 'cookievalue ', {expires: 7, path:'/', domain: 'souvc. com ', secure: false, raw: false });
Parameter description:
1). expires: 365
Defines the effective time of a cookie. The value can be a number (counted from the cookie creation time, in days) or a Date object. If this parameter is omitted, the created cookie is a session cookie and will be deleted when the user exits the browser.
// 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 ).
Expires: (Number | Date) validity period. You can set an integer as the validity period (in days) or a Date object as the Cookie's expiration Date. If the specified date is negative, the cookie will be deleted. If it is not set or set to null, the cookie will be processed as a Session Cookie and deleted after the browser is closed.
Var COOKIE_NAME = 'username'; if ($. cookie (COOKIE_NAME) {$ ("# username "). val ($. cookie (COOKIE_NAME);} $ ("# check "). click (function () {if (this. checked) {$. cookie (COOKIE_NAME, $ ("# username "). val (), {path: '/', expires: 10}); // var date = new Date (); // date. setTime (date. getTime () + (3*24*60*60*1000); // It expires three days later. // $. cookie (COOKIE_NAME, $ ("# username "). val (), {path: '/', expires: date});} else {$. cookie (COOKIE_NAME, null, {path: '/'}); // Delete cookie }});
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 cookie that defines 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 that creates the cookie.
3). domain: the domain Name of the webpage where the cookie is created;
4 ). secure: The default value is false. If the value is true, the cookie transmission protocol must be https. raw: The default value is false. The data is automatically encoded and decoded during reading and writing. (encodeURIComponent encoding is used, decodeURIComponent). disable this function. Set it to true.
3. Delete the cookie.
$. Cookie (the _ cookie, null); // delete a cookie
$. Cookie ('cookiename', null, {path: '/'}); // Note: to delete a cookie with a valid path
Iii. Usage
First, it contains the library file of jQuery, followed by the library file of jquery. cookie. js.