Home > Web Front-end > JS Tutorial > Use of jquery.cookie() method (read, write, delete)_jquery

Use of jquery.cookie() method (read, write, delete)_jquery

WBOY
Release: 2016-05-16 17:10:29
Original
1491 people have browsed it

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

Configuration of jquery.cookie.js

Contains jQuery library file first, and jquery.cookie.js library file later.






Usage


1. Add a new session cookie:

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

Note: When the cookie validity period is not specified, the created cookie will be valid until the user closes the browser by default, so it is called

"session cookie".

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

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

Note: When the cookie validity period is specified, the cookie created is called a "persistent cookie".

3. Create a cookie and set the valid path of 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 the cookie set by another page, you must set the cookie path. The path to the cookie is used to set the top-level directory that can read the cookie. Setting this

path as the root directory of the website allows all web pages to read each other's cookies (generally do not set this to prevent conflicts).

4. Read cookie:

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

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

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

$.cookie('the_cookie', null);

----------Explanation of related parameters---------------

1).expires: 365

Defines the validity period of the cookie. The value can be a number (in days from the time the cookie is 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.

Define the valid path of the cookie. By default, the value of this parameter is the path to the web page that created the cookie (standard browser behavior).

If you want to access this cookie throughout the website, you need to set the effective 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: 'example.com'

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

3).secure: true

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

4).raw: true

Default value: false.

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template