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> |