cookie manipulation JQuery

The jQuery library file is included first, and the jquery.cookie.js library file is included later. 

<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script> 

<script type="text/javascript" src="js/jquery.cookie.js"> </script> 

Using method 1. Add a new session cookie:  $.cookie('the_cookie', 'the_value');  Note: When the validity period of the cookie is not specified, the validity period of the created cookie is by default until the user closes the browser. So called  "session cookie (session cookie)". 2. Create a cookie and set the valid time to 7 days:  $.cookie('the_cookie', 'the_value', { expires: 7 });  Note: When the valid time of the cookie is specified, the created cookie is called " Persistent cookies". 3. Create a cookie and set a valid path to the cookie:  $.cookie('the_cookie', 'the_value', { expires: 7, path: '/' }); Note: By default, only the web page that sets the cookie can read the cookie. If you want a page to read a cookie set , the path to the cookie must be set. The cookie's path is used to set the top-level directory from which cookies can be read. put this  























The 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 exists => 'the_value' 

$.cookie('not_existing'); // cookie does not exist => null 

5. Delete cookie, by passing null as The value of the cookie can be: 

$.cookie('the_cookie', null); 

---------- Explanation of relevant parameters--------------- 

1).expires : 365 

defines the validity time of the cookie, the value can be a number (in days since the cookie was created) or a Date 

object . If omitted, the cookie created is a session cookie and will be deleted when the user exits the browser. 

2).path: '/' 

Default: Only the web page that sets the cookie can read the cookie. 

Defines a valid path for cookies. By default, the value of this parameter is the path to the web page where the cookie was created (standard browser behavior). 

If you want to access this cookie in the whole website you need to set a valid path like this: 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: 

Default: The domain name owned by the web page that created the cookie. 

3).secure: true 

Default value: false. If true, the transmission of the cookie needs to use a secure protocol (HTTPS). 

4).raw: true 

Default value: false. 

By default, encoding and decoding are performed automatically when reading and writing cookies (encode with encodeURIComponent, 

decode with decodeURIComponent). To turn off this feature set raw: true.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326777972&siteId=291194637