Cookie-based storage instance-JavaScript

Source: Internet
Author: User
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.

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.