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

Source: Internet
Author: User
A lightweight cookie plugin that can read, write, and delete cookies.

Configuration of Jquery.cookie.js

First contains the library file for jquery, followed by the Jquery.cookie.js library file.

<script type= "Text/javascript" src= "Js/jquery-1.6.2.min.js" ></script>

<script type= "Text/javascript" src= "Js/jquery.cookie.js" ></script>

How to use

1. Add a new session cookie:

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

Note: When a cookie is not valid, the validity of the cookie is created by default until the user closes the browser, so it is called

"Session Cookie".

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

$.cookie (' The_cookie ', ' The_value ', {expires:7});

Note: When the cookie is valid, the cookie created is called the persistent cookie (persistent cookie).

3. Create a cookie and set a valid path for 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 one page to read another page set

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

The path is set to the root of the Web site, allowing all pages to read cookies to each other (not normally set to prevent conflicts).

4. Read cookies:

$.cookie (' The_cookie '); Cookies exist => ' the_value '

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

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

$.cookie (' The_cookie ', null);

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

1). expires:365

Defines the valid time for a cookie, which can be a number (from the time the cookie was created, in days), or a date pair

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

2). Path: '/'

By default: Only the Web page that sets the cookie can read the cookie.

Defines a valid path for a cookie. By default, the value of this parameter is the path of the Web page where the cookie was created (the behavior of the 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

A valid path cookie, you need to include this path when calling the function: $.cookie (' The_cookie ', NULL,

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

Default value: The domain name of the Web page that created the cookie.

3). secure:true

Default value: False. If the transport for True,cookie requires the use of security Protocol (HTTPS).

4). raw:true

Default value: False.

By default, encoding and decoding is done automatically when the cookie is read and written (using encodeURIComponent encoding,

decodeURIComponent decoding). To turn off this feature setting raw:true.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.