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. <script type="text/javascript" src="js/jquery.min.js"></script>

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

use:

1. Add a "session cookie"

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

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

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

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

The valid time of the cookie is specified here, and the cookie created is called a "persistent cookie". Note that the unit is: days;

3. Create a cookie and set the effective path of the cookie

$.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 one page to read the cookie set on another page, you must set the cookie path. The cookie path is used to set the top-level directory that can read the cookie. Set this path as the root directory of the website, so that all web pages can read cookies from each other (generally don't set this way to prevent conflicts).

4. Read the cookie

$.cookie('the_cookie');

5. Delete cookies

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

6. Optional parameters

 
  1. $.cookie('the_cookie','the_value',{

  2. expires:7,

  3. path:'/',

  4. domain:'jquery.com',

  5. secure:true

  6. })

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

Note: Cookies are stored based on the domain name. It will take effect only when it is placed on the test server or on the local localhost server. Cookies have the feature of not being shared under different domain names. Simply opening an html page locally is invalid.

Guess you like

Origin blog.csdn.net/qq_41071754/article/details/110244721