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! ");}});});