JavaScript implements a cookie-based storage class instance

Source: Internet
Author: User
Tags cookie names

The specific analysis is as follows:

Through this JS class, you can use the same as the session cookie, very simple!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105-106 * * Cookiestorage.js * This class implements the same storage API * as Localstorage and Sessionstorage, which is based on HTTP cookies. */function Cookiestorage (maxage, path) {///two parameters represent storage duration and scope//Get an object to store all cookies var cookies = (function () {//Type description before The GetCookies function var cookies = {}; The object will eventually return var all = Document.cookie; Obtain information about all cookies in the form of a large string if (all = = "")//If the property is a blank return cookie; Returns an empty object var list = All.split (";"); Separation of known/value pairs for (var i = 0; i < list.length; i++) {//traverse each cookie var cookie = list[i]; var p = cookie.indexof ("=");//Find To the first "=" symbol var name = cookie.substring (0,p); Gets the name of the cookie var value = cookie.substring (p+1); Gets the value of the cookie corresponding to = decodeuricomponent (value); Decoding its value cookies[name] = value; Store the name value pair in the object to return cookies; }()); Store the names of all cookies in an array of the var keys = []; for (var key in cookies) Keys.push (key); Now define the storage API public properties and methods//number of cookies stored this.length = keys.length; Returns the name of the nth cookie, and returns null This.key = function (n) {if (N < 0 | | | n >= keys.length) return NULL if n crosses the line keys[n]; }; Returns the cookie value for the specified name, or null This.getitem = function (name) {return Cookies[name] | | null; Store cookie Value This.setitem = function (key, value) {if (!) ( Key in Cookies) {//If the cookie to be contributed does not exist Keys.push (key);//Add the specified name to the array where all the cookie names are stored this.length++;//The number of cookies plus one}//Add the name/ Values are stored in the cookie object for the data. Cookies[key] = value; Start to formally set up cookies. First encode the value of the cookie to be stored//create a string var cookie = key + "=" + encodeuricomponent (value) in the form of "NAME = encoded value"; Add the properties of the cookie to the string if (maxage) cookie + = "; Max-age= "+ maxage; if (path) cookie = "; Path= "+ path; Set the cookie Document.cookie = Cookie by Document.cookie property; }; Deletes the specified cookie This.removeitem = function (key) {if (!) ( Key in the cookie)) return; If the cookie does not exist, do nothing//remove the specified cookie from the internal maintenance cookies Delete Cookies[key]; Also deletes the name in the cookie inside the array. It is simpler to use the array indexof () method defined by ES5. for (var i = 0; i < keys.length i++) {//Traverse all names if (keys[i] = = key) {//When we find the Keys.splice (i,1) we are looking for;//Remove it from the array. Break }} this.length--; Number of cookies minus one//finally by setting the value of the cookie toThe empty string//and the expiration date set to 0来 deletes the specified cookie. Document.cookie = key + "=; Max-age=0 "; }; Remove All Cookies this.clear = function () {//loop all cookies by name and delete cookies for (var i = 0; i < keys.length; i++) document. Cookie = Keys[i] + "=; Max-age=0 "; Resets all internal state cookies = {}; Keys = []; this.length = 0; }; }
Related Article

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.