JQuery read write Delete cookie specific instance

Source: Internet
Author: User

In jquery, the cookie operation is not as original as in js. jquery directly provides the jquery cookie plug-in. Below I will introduce some common cookie operation methods in jquery, for more information, see.

JQuery cookie is a good cookie plug-in. The general usage is as follows:

The Code is as follows: Copy code

Example $. cookie ('name', 'value ');

Set the cookie value and set the value of the name variable to value.

The Code is as follows: Copy code

Example $. cookie ('name', 'value', {expires: 7, path: '/', domain: 'jquery. com', secure: true });

Create a cookie, including the Domain Name of the validity period.

The Code is as follows: Copy code

Example $. cookie ('name', 'value ');

Create cookie

The Code is as follows: Copy code

Example $. cookie ('name', null );

Delete a cookie

Var account = $. cookie ('name ');

Get a cookie (name) value to myvar

Example

The Code is as follows: Copy code

$. Cookie ('name', 'value'); // set the cookie value and set the value of the name variable to value.
$. Cookie ('name', 'value', {expires: 7, path: '/', domain: 'jquery. com ', secure: true}); // create a cookie, including the Domain Name of the validity period path.
$. Cookie ('name', 'value'); // create a cookie
$. Cookie ('name', null); // delete a cookie
Var account = $. cookie ('name'); // get a cookie (name) value to myvar

Example 2

The Code is as follows: Copy code

<Script type = "text/javascript">
$ (Function (){
Var COOKIE_NAME = 'test _ cooker ';
// Set the cookie through the interval
$ ('A'). eq (0). click (function (){
$. Cookie (COOKIE_NAME, 'test', {path: '/', expires: 1 });
Return false;
});
// Set the cookie and expiration time
$ ('A'). eq (1). click (function (){
Var date = new Date ();
Date. setTime (date. getTime () + (1x24x60*60*1000 ));
$. Cookie (COOKIE_NAME, 'test', {path: '/', expires: date });
Return false;
});
// Obtain the cookie
$ ('A'). eq (2). click (function (){
Alert ($. cookie (COOKIE_NAME ));
Return false;
});
// Delete the cookie
$ ('A'). eq (3). click (function (){
$. Cookie (COOKIE_NAME, null, {path :'/'});
Return false;
});
});
</Script>

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.