This article mainly introduces how to implement the Cookie-based storage class in JavaScript. The example analyzes the data storage skills in javascript through cookies, which is very useful, for more information about how to use JavaScript to implement Cookie-based storage, see the following example. Share it with you for your reference. The specific analysis is as follows:
Through this JS class, you can use cookies like session, which is very simple!
/** CookieStorage. js * this class implements the same storage API as localStorage and sessionStorage *. it is implemented based on HTTP Cookies. */function CookieStorage (maxage, path) {// The two parameters indicate the storage validity period and scope. // Obtain the object var cookies for storing all cookies = (function () {// type the previously introduced getCookies function var cookies ={}; // this object will eventually return var all = document. cookie; // obtain information about all cookies in the form of an uppercase string if (all = "") // return cookies if this attribute is a blank character; // return an empty object var list = all. split (";"); // famous for separation /Value pair for (var I = 0; I <list. length; I ++) {// traverse each cookie var cookie = list [I]; var p = cookie. indexOf ("="); // find the first "=" Symbol var name = cookie. substring (0, p); // Obtain the cookie name var value = cookie. substring (p + 1); // obtain the value of cookie value = decodeURIComponent (value); // decode the value of cookie [name] = value; // store the name-value pair in the object} return cookies;} (); // store all cookie names in an array var keys = []; for (var key in cookies) keys. pus H (key); // now defines the number of cookies stored in the common attributes and methods of the stored API. this. length = keys. length; // return the name of the nth cookie. if n is out of bounds, null this is returned. key = function (n) {if (n <0 | n> = keys. length) return null; return keys [n] ;}; // return the cookie value of the specified name. if it does not exist, null this is returned. getItem = function (name) {return cookies [name] | null ;}; // store the cookie value this. setItem = function (key, value) {if (! (Key in cookies) {// if the cookie to be promoted does not exist keys. push (key); // add the specified name to the array storing all cookie names this. length ++; // The number of cookies plus one} // store the data of this/value pair to the cookie object. cookies [key] = value; // start to set cookies. // Encode the value of the cookie to be stored // create a string in the form of "name = encoded value" var cookie = key + "=" + encodeURIComponent (value); // add the cookie attributes to the string if (maxage) cookie + = "; max-age =" + maxage; if (path) cookie + = "; path = "+ path; // use document. Cookie attribute to set cookie document. cookie = cookie;}; // delete the specified cookie this. removeItem = function (key) {if (! (Key in cookies) return; // if the cookie does not exist, nothing will be done. // delete the specified cookie delete cookies from the internally maintained cookies group [key]; // delete the cookie name in the internal array. // It is easier 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); // delete it from the array. break;} this. length --; // reduce the number of cookies by one // The cookie value is set to an empty string // and the validity period is set to 0 to delete the specified cookie. document. cookie = key + "=; max-age = 0" ;}; // delete all cookies this. clear = function () {// loop the names of all cookies and delete the cookies for (var I = 0; I <keys. length; I ++) document. cookie = keys [I] + "=; max-age = 0"; // reset all internal cookies ={}; keys = []; this. length = 0 ;};}
I hope this article will help you design javascript programs.