JS Operation Cookie

Source: Internet
Author: User

$ (function () {/** * version 1.0 * Feature: This file is intended to operate cookies via JS, including read and write cookies * Guo June weeks * email:[email protected] * Use example: * window. __cookie.setcookie (String key,string value,int time); * Window.__cookie.getcookie (string key); * Note: * 1:cookie is saved as a string in Document.cookie * The 2:window object holds a read-only reference to the Document object. This determines that we can obtain this cookie string through the Window.document.cookie * method, but it is not possible to assign a value to a cookie in this way. That is: * Window.document.cookie = ' aaa '; Does not change the cookie * At this time, we should pass Document.cookie = ' AAA '; The way to make changes to the cookie * 3: A few notes on the expiry time of the cookie: * 3.1: This time, in "milliseconds", 1000ms = 1s; * 3.2: only if the "key value pair" in the cookie has not expired, document.cookie Has the string form of the key-value pair. * In short, when obtaining a cookie, you do not need to manually verify the expiration time, because if it expires, you will not get this value. * 3.3: As to, the key value pair, whether the expiration, and the expiration of processing, is implemented by the browser itself. * 4: Note on Key,value,expire when setting cookies: * 4.1: Recommended Window.__cookie.setcookie (String,string,int). The following method of invocation may cause a variety of inexplicable problems, not recommended. * Window.__cookie.setcookie (1,2,30000); * Window.__cookie.setcookie (', 2,30000); * Window.__cookie.setcookie (2, ', 30000); * Window.__cookie.setcookie (3, ', 30000); * Window.__cookie.setcookiE (' _ ', ', 30000); * Window.__cookie.setcookie (-1, ', 30000); * Window.__cookie.setcookie ('; ', ', 30000); * 4.2:setcookie and GetCookie parameter description: * There is no excessive validation of its parameters, such as: Key should satisfy the naming conventions of variables. Because PHP does have the ability to handle associative arrays. **/Window.__cookie = {' cookieenabled ': window.navigator.cookieEnabled, ' Setcookie ': function (key,value,expire) {if (! this.cookieenabled) {alert (' Browser not open or cookie function! '); return false;} var exdate = new Date (); Exdate.settime (Exdate.gettime () +expire); var c_nvalue=escape (value) + ((expire==null)? "" : "; Expires= "+exdate.toutcstring ()); if (This.getcookie (key) = = null) {Document.cookie = key + "=" + C_nvalue + document.cookie;} else{var c_value = Window.document.cookie; var c_start = C_value.indexof ("" + key + "="); if (C_start = =-1) {C_start = C_value.indexof (key + "="),} C_start = C_value.indexof ("=", C_start) + 1; var c_end = C_value.indexof (";", C_start); if (c_end = =-1) {c_end = c_value.length;} document.cookie = C_value.substring (0,c_start) + C_nvalue + c_value.substring (c _end,c_value.length); }}, ' GetCookie ': function (c_name) {if (!this.cookieenabled) {alert (' Browser not open or cookie function! '); return false;} var c_value = wi Ndow.document.cookie; var C_start = C_value.indexof ("" + c_name + "="); if (C_start = =-1) {C_start = C_value.indexof (c_name + "=");} if (C_start = =-1) {c_value = null;} else{C_start = c_value.indexof ("=", C_start) + 1; var c_end = C_value.indexof (";", C_start); if (c_end = = 1) {c_end = C_v Alue.length; } C_value = Unescape (c_value.substring (c_start,c_end)); } return c_value; } }});

JS Action Cookie

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.