Analysis of jquery. cookie usage

Source: Internet
Author: User
Tags set cookie

Analysis of jquery. cookie usage

This article mainly analyzes the usage of jquery. cookie in detail. If you need it, you can refer to it and hope to help you.

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.6.2.min.js"> </script>

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

 

 

Usage

 

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. Therefore, it is called "session cookie )"

 

 

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 )".

 

 

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 a page to read the cookie set for another page, you must set the cookie Path.

 

The cookie Path is used to set the top-level directory that can read cookies. Set this path to the root directory of the website, so that all webpages can read cookies from each other (do not set it like this to prevent conflicts)

 

 

Read cookie:

 

$. Cookie (the _ cookie ');

 

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

 

 

Delete the cookie and pass null as the cookie value:

 

$. Cookie (the _ cookie, null );

 

 

Description of Related Parameters

 

Expires: 365

 

Defines the effective time of a cookie. The value can be one (calculated from the cookie creation time, in days) or one Date.

 

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

 

 

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

 

 

Secure: true

 

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

 

 

Raw: true

 

Default Value: false. By default, the system automatically performs encoding and decoding (encodeURIComponent encoding and decodeURIComponent decoding) when reading and writing cookies ).

 

To disable this function, set raw: true.

 

 

$. Cookie (the _ cookie); // get cookie $. cookie (the _ cookie, the _ value); // set cookie $. cookie (the _ cookie, the _ value, {expires: 7}); // set cookie with an expiration date seven days in the future $. cookie ('the _ cookies', '', {expires:-1}); // delete cookie

$. Cookie (the _ cookie, null); // delete cookie

 

 

$. Cookie (the _ cookie, the _ value, {expires: 7, path: '/', domain: '80tvb. com ', secure: true}); // call Method

 

// Or like this: $. cookie (the _ cookie, the _ value ');

 

// Delete Cookie: $. cookie (the _ cookie, null );

 

 

 

The following describes how to use jQuery to operate the cookie plug-in:

 

$. Cookie (the _ cookie); // read the Cookie value

$. Cookie (the _ cookie, the _ value); // set the cookie value

$. Cookie (the _ cookie, the _ value, {expires: 7, path: '/', domain: 'jquery. com ', secure: true}); // create a cookie, including the Domain Name of the validity period path.

$. Cookie (the _ cookie, the _ value); // create a cookie

$. Cookie (the _ cookie, null); // delete a cookie

 

 

Jquery sets cookie expiration time and checks whether cookies are available

 

Let cookies expire in x minutes

Var date = new date ();

Date. settime (date. gettime () + (x * 60*1000 ));

$. Cookie ('example ', 'foo', {expires: date });

 

$. Cookie ('example ', 'foo', {expires: 7 });

 

 

Check whether cookies are available

$ (Document ). ready (function () {var dt = new date (); dt. setseconds (dt. getseconds () + 60); document. cookie = "cookietest = 1; expires =" + dt. togmtstring (); var cookiesenabled = document. cookie. indexof ("cookietest = ")! =-1; if (! Cookiesenabled) {// cookies cannot be used ........}});

 

 

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.