JQuery. cookie. js usage, jquery. cookie. js

Source: Internet
Author: User

JQuery. cookie. js usage, jquery. cookie. js

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

Jquery. cookie. js Configuration

First, it contains the library file of jQuery, followed by the library file of jquery. cookie. js.

<Script type = "text/javascript" src = "js/jquery-1.8.1.min.js"> </script>

<Script type = "text/javascript" src = "js/jquery. cookie. js"> </script>

Usage:

Cookie has four different attributes: name, content, domain, and path.

1. Add a new session cookie:

$. Cookie (the _ cookie, the _ value ');

Note: When the cookie validity period is not specified, the created cookie is valid until the user closes the browser by default.

"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 created cookie is called "persistent cookie )".

3. Create a cookie and set the valid cookie Path:

$. Cookie (the _ cookie, the _ value, {expires: 7, path :'/'});

Note: by default, only webpages with cookie settings can read the cookie. If you want to read one page from another page

The path of the cookie. The cookie Path is used to set the top-level directory that can read cookies. Set this

PATH is set as the root directory of the website, so that all webpages can read cookies from each other (generally do not set this way to prevent conflicts ).

4. Read cookie:

$. Cookie (the _ cookie); // cookie existence => 'the _ value'

$. Cookie ('not _ existing ');//Cookie does not exist => null

5. Delete the cookie and pass null as the cookie value:

$. Cookie (the _ cookie, null );

---------- Description of relevant parameters ---------------

1). expires: 365

Defines the cookie validity period. The value can be a number (counted from the cookie Creation Time,In days) Or a Date pair

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

2). path :'/'

Default: Only webpages with cookie settings can read the cookie.

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

If you want to access this cookie on the entire website, you need to set the valid path as follows: path :'/'. If you want to delete a definition

Cookie with 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 of the webpage for which the cookie is created.

3). secure: true

Default Value: false. If the value is true, the secure protocol (HTTPS) is required for cookie transmission ).

4). raw: true

Default Value: false.

By default, the system automatically performs encoding and decoding when reading and writing cookies (using encodeURIComponent encoding,

DecodeURIComponent decoding ). To disable this function, set raw: true.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.