Jquery. Cookie of method use of of read write delete

  • 2020-03-30 00:43:33
  • OfStack

A lightweight cookie plug-in, can read, write, delete cookies.

Jquery. Cookies. Js configuration

The library file for jQuery is included first, followed by the library file for jQuery. Cookie. Js.

< Script type = "text/javascript" SRC = "js/jquery - 1.6.2. Min. Js" > < / script>

< Script type = "text/javascript" SRC = "js/jquery. Cookies. Js" > < / script>

Method of use

1. Add a new session cookie:

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

Note: when the cookie is not specified, the cookie is created until the user closes the browser by default, so it is called

"Session cookie."

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

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

Note: the cookie created when the valid time of the cookie is specified is called a "persistent cookie".

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

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

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

Set the cookie, you must set the path of the cookie. The path to the cookie is used to set the top-level directory that can read the cookie. Will this

Four paths are set to the root of the site, so that all pages can read cookies from each other (this is generally not set to prevent conflicts).

4. Read cookie:

$. Cookies (' the_cookie '); // cookie exists => 'the_value'

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

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

$. Cookies (' the_cookie, null);

-- explanation of relevant parameters --

1) expires: 365

Define the duration of a cookie, which can be a number (in days from the time the cookie was created) or a Date pair

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

2.) path: '/'

Default: the cookie can only be read by the web page where the cookie is set.

Defines a valid path to a cookie. By default, the value of this parameter is the path of the web page where the 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: path: '/'. If you want to delete a definition

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

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

Default: the domain name of the web page where the cookie is created.

3). Secure: true

The default value is false. If true, the transmission of the cookie needs to use the secure protocol (HTTPS).

4). Raw: true

The default value is false.

By default, cookies are automatically encoded and decoded when read and written (encodeURIComponent encoding is used,

DecodeURIComponent). To turn this off set raw: true.

Related articles: