Usage of jQuery. cookie. js and explanation of related parameters

  • 2021-07-26 06:48:12
  • OfStack

cookie operation needs to be uploaded to the server to view the results. No local

1 lightweight cookie plug-in, which can read, write and delete cookie.

Configuration of jquery. cookie. js

Include the library files for jQuery first, followed by the library files for jquery. cookie. js.


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

Usage

1. Add a new session cookie:


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

Note: When the cookie expiration time is not specified, the created cookie expires by default until the user closes the browser, so it is called

"Session cookie (session cookie)".

2. Create an cookie and set the valid time to 7 days:


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

Note: The cookie created when the cookie effective time is specified is referred to as "Persistent cookie (persistent cookie)".

3. Create an cookie and set a valid path to cookie:


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

Note: By default, the cookie can only be read from a Web page where cookie is set. If you want one page to read another page, set

To set cookie, the path of cookie must be set. The path to cookie is used to set the top-level directory where cookie can be read. Put this

Path is set as the root directory of the website, so that all web pages can read cookie from each other (1. Do not set this to prevent conflicts).

4. Read cookie:


$.cookie('the_cookie'); // cookie Existence  => 'the_value' 
$.cookie('not_existing'); // cookie Nonexistent  => null

5. Delete cookie by passing null as the value of cookie:


$.cookie('the_cookie', null);

--Interpretation of the relevant parameters---

1).expires: 365

Defines the effective time of cookie, which can be 1 number (in days from the time cookie was created) or 1 Date pair

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

2).path: '/'

Default: cookie can only be read from a Web page where cookie is set.

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

If you want to access this cookie throughout the site, you need to set a valid path like this: path: '/'. If you want to delete 1 definition

cookie with a valid path, which you need to include when calling the function: $. cookie ('the_cookie', null,


{ path: '/' }); .  domain: 'example.com'

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

3).secure: true

Default: false. In the case of true, cookie transmission requires the use of a secure protocol (HTTPS).

4).raw: true

Default: false.

By default, cookie is encoded and decoded automatically when it is read and written (using encodeURIComponent encoding,

decodeURIComponent decoding). To turn this off, set raw: true.


Related articles: