JavaScript implements storage class instances based on Cookie

  • 2020-05-27 04:12:27
  • OfStack

This article demonstrates an example of an JavaScript implementation of an Cookie-based storage class. Share with you for your reference. The specific analysis is as follows:

With this JS class, you can use cookie just as you would session1. Very simple!


/*
 * CookieStorage.js
 *  This class implements the image localStorage and sessionStorage1 Sample storage API
 *  The difference is, it's based on HTTP Cookies Implementation of the .
 */
function CookieStorage(maxage, path) {
//  The two parameters represent the storage expiration and scope, respectively 
 //  To obtain 1 Total storage cookies The object of 
 var cookies = (function() {
 //  Type described earlier getCookies function 
  var cookies = {};
  //  The object will eventually return 
  var all = document.cookie;
  //  Get all as a large string cookies The information of 
  if (all === "")
  //  If the property is a blank character 
   return cookies;
   //  return 1 An empty object 
  var list = all.split("; ");
  //  The separation of famous / The value of 
  for(var i = 0; i < list.length; i++) {
  //  Through each cookie
   var cookie = list[i];
   var p = cookie.indexOf("=");
   //  Find the first 1 A" = "Symbol 
   var name = cookie.substring(0,p);
   //  To obtain cookie The name of the 
   var value = cookie.substring(p+1);
   //  To obtain cookie The value of the corresponding 
   value = decodeURIComponent(value);
   //  The value is decoded 
   cookies[name] = value;
   //  Stores name-value pairs in an object 
  }
  return cookies;
 }());
 //  Will all cookie The name is stored to 1 An array of 
 var keys = [];
 for(var key in cookies) keys.push(key);
 //  Now define store API Common properties and methods 
 //  Storage of cookies The number of 
 this.length = keys.length;
 //  Returns the first n a cookie The name if n Cross the line and return null
 this.key = function(n) {
  if (n < 0 || n >= keys.length) return null;
  return keys[n];
 };
 //  Returns the specified name cookie Value, return if it does not exist null
 this.getItem = function(name){
 return cookies[name] || null;
 };
 //  storage cookie value 
 this.setItem = function(key, value) {
  if (!(key in cookies)) {
  //  If you want to make it happen cookie Also there is no 
   keys.push(key);
   //  Adds the specified name to the store all cookie In an array of names 
   this.length++;
   // cookies The number of add 1
  }
  //  The name / The value pair data is stored into cookie In the object .
  cookies[key] = value;
  //  Start the formal setup cookie.
  //  The first one that's going to be stored cookie The value of 
  //  At the same time to create 1 "The name, = A string in the form of an encoded value 
  var cookie = key + "=" + encodeURIComponent(value);
  //  will cookie Is also added to the string 
  if (maxage) cookie += "; max-age=" + maxage;
  if (path) cookie += "; path=" + path;
  //  through document.cookie Property to set cookie
  document.cookie = cookie;
 };
 //  Delete specified cookie
 this.removeItem = function(key) {
  if (!(key in cookies)) return;
  //  if cookie If you don't exist, you don't do anything. Right 
  //  Maintained from within cookies Group deletes the specified cookie
  delete cookies[key];
  //  At the same time will cookie The name in the array is also deleted from the internal array .
  //  If you are using ES5 Defined array indexOf() The method is much simpler .
  for(var i = 0; i < keys.length; i++) {
  //  Go through all the names 
   if (keys[i] === key) { 
   //  When we found the one we were looking for 
    keys.splice(i,1); 
 //  Remove it from the array .
    break;
   }
  }
  this.length--; 
  // cookies The number of reduction 1
  //  Final approval will be given cookie Is set to an empty string 
  // And set the expiration date to 0 To delete the specified cookie.
  document.cookie = key + "=; max-age=0";
 };
 //  Delete all cookies
 this.clear = function() {
  //  Loop all cookies The name and will cookies delete 
  for(var i = 0; i < keys.length; i++)
   document.cookie = keys[i] + "=; max-age=0";
  //  Reset all internal states 
  cookies = {};
  keys = [];
  this.length = 0;
 };
}

I hope this article has helped you with your javascript programming.


Related articles: