Home > Web Front-end > JS Tutorial > body text

Detailed explanation on how to use jQuery.cookie.js and related parameters

小云云
Release: 2018-01-23 13:40:58
Original
1484 people have browsed it

This article mainly shares with you the usage of a lightweight cookie plug-in, which can read, write, and delete cookies. This article mainly introduces the usage of jQuery.cookie.js and the explanation of related parameters. Friends who need it can refer to it. I hope it can help everyone.

The cookie operation needs to be uploaded to the server to view the results. It cannot be viewed locally.

Configuration of jquery.cookie.js

First include the jQuery library file, and then include Library file of 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>
Copy after login

Usage

1. Add a new session cookie:

$.cookie('the_cookie', 'the_value');
Copy after login

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

"session cookie (session cookie)".

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

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

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

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

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

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 these

paths 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 the cookie:

$.cookie('the_cookie'); // cookie存在 => 'the_value' 
$.cookie('not_existing'); // cookie不存在 => null
Copy after login

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

$.cookie('the_cookie', null);
Copy after login

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

1).expires: 365

Define the validity time of the cookie, the value can be a number ( (measured in days since the cookie was 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 the path when calling the function: $.cookie('the_cookie', null,

{ path: '/' });。 domain: 'example.com'
Copy after login

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

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 automatically performed when reading and writing cookies (use encodeURIComponent to encode,

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

Related recommendations:

jQuery combined with jQuery.cookie.js plug-in to implement skin changing function example

jQuery+jQuery. Sample code for cookie.js plug-in to implement skin change function

Problems encountered when using Jquery.cookie.js

The above is the detailed content of Detailed explanation on how to use jQuery.cookie.js and related parameters. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!