How to use jquery.cookie.js

Cookies

Definition: a technology that allows the website server to store a small amount of data on the client's hard disk or memory, and read data from the client's hard disk;

Download and import: jquery.cookie.js is based on jquery; first import jquery, then import: jquery.cookie.js; download: http://plugins.jquery.com/cookie/

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

1. Add a "session cookie"

1
$.cookie( 'the_cookie' 'the_value' );

The validity period of the cookie is not specified here, and the created cookie is valid until the user closes the browser by default, so it is called "session cookie (session cookie)".

2. Create a cookie and set the validity period to 7 days

1
$.cookie( 'the_cookie' 'the_value' , { expires: 7 });

The validity period of the cookie is specified here, and the created cookie is called a "persistent cookie". Note that the unit is: days; (can be changed to other)

3. Create a cookie and set a valid path to the cookie

1
$.cookie( 'the_cookie' 'the_value' , { expires: 7, path:  '/'  });

By default, only the web page that sets the cookie can read the cookie. If you want a page to read a cookie set by another page, 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. Setting this path as the root directory of the website allows all web pages to read cookies from each other (generally do not set this to prevent conflicts).

4. Read cookies

1
$.cookie( 'the_cookie' );

5. Delete cookies

1
$.cookie( 'the_cookie' null );    //通过传递null作为cookie的值即可

6. Optional parameters

1
2
3
4
5
6
$.cookie( 'the_cookie' , 'the_value' ,{
     expires:7,  
     path: '/' ,
     domain: 'jquery.com' ,
     secure: true
}) 

expires : (Number|Date) validity period; when an integer is set, the unit is days; a date object can also be set as the expiration date of the cookie;
path : (String) the page path where the cookie was created;
domain : (String) the cookie was created The domain name of the page;
secure : (Booblean) If set to true, then the transmission of this cookie will require a secure protocol, such as: HTTPS;

 

session:

temporary storage

sessionStorage.name="boss"; //Storage string
// console.log(sessionStorage.name); 
 
sessionStorage.info=['admin','boss']; //Storage array/object string
// console.log(sessionStorage.info);  

// sessionStorage.setItem('key','val'); //Store data by method
// console.log(sessionStorage);

// sessionStorage.clear(); //Clear data
// console.log(sessionStorage);
 
long term storage
// localStorage.name = 'Smith';
// console.log(localStorage.name);

// localStorage.info=['Smith','boss'];
// console.log(localStorage.info);

// localStorage.setItem('name','cmh');
// console.log(localStorage);

// localStorage.clear();
// console.log(localStorage);
 
Storing and reading objects and arrays
object
j={'name':'cmh',age:22};
sessionStorage.info = JSON.stringify(j)
a = JSON.parse(sessionStorage.info);
console.log(a.age);
array
arr =[111,222,333];
sessionStorage.info = JSON.stringify(arr)
a = JSON.parse(sessionStorage.info);
console.log(a[1]);
 
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324595100&siteId=291194637