Use of jquery. cookie

Source: Internet
Author: User

Use of jquery. cookie
I remember that native js operations on cookies are not very troublesome, but it seems that jquery is simpler. However, compared with native js, it seems that it is not good to introduce two additional files, but since your project is based on jquery, use jquery. cookie is good. The jquery. cookie Project address: jquery-cookie readme. md also describes usage. After a brief introduction, we will use it with an instance: Add: $. cookie ('isza', '1', {expires: 1, path: '/'}); Number 1 indicates that the validity period is set to 1 day, path: '/' indicates the root directory of the current domain name, for example, www.baidu.com, so that the cookie value can be used after being set. Delete: \ $. cookie ('iszany', null); or \ $. removeCookie ('iszany'); other related parameters: 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. For details, see the official website. For more information, see jquery. How to Use Cookies [instance]. I want to click Set cookie and click Delete to delete the cookie. Page: 1684 our html code is:

<Body> <a href = "#" class = "zan"> thumb ups </a> <a href = "#" class = "del"> Delete cookies </a> <script src = "http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"> </script> <script src = "jquery. cookie. js "> </script> </body>

 

Js Code
$ (Function () {$ ('. zan '). click (function () {// first judge whether the cookie is equal to the value set by us. // if it is not equal to the value set, send the request if ($. cookie ('iszance') = undefined) {$. post ('add. php ', {id: "1"}, function (data) {if (data = "OK") {// if the database is successfully written with 1, add cookie cache $. cookie ('iszany', '1', {expires: 1}) ;}}; // If so, 1 is not added, the prompt "else {alert (" I liked it once today. Thank you! ") ;}}); // Delete cookie $ ('. del'). click (function () {if ($. cookie ('isza ')! = Undefined) {// $. cookie ('isza', null); $. removeCookie ('isza'); alert ("your cookie value has been deleted successfully! ");}});});

 

 

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.