SoFunction
Updated on 2025-04-03

JavaScript implements cookie-based storage class instance

This article describes the implementation of a cookie-based storage class in JavaScript. Share it for your reference. The specific analysis is as follows:

Through this JS class, you can use cookies like using session, which is very simple!

/*
  *
  * This class implements storage APIs like localStorage and sessionStorage
  * The difference is that it is implemented based on HTTP cookies.
  */
function CookieStorage(maxage, path) {
// Two parameters represent the storage validity period and scope respectively // Get an object that stores all cookies var cookies = (function() {
 // The getCookies function introduced before the type  var cookies = {};
  // The object will eventually return  var all = ;
  // Get information about all cookies in the form of a large string  if (all === "")
  // If the property is a blank   return cookies;
   // Return an empty object  var list = ("; ");
  // Separate famous/value pairs  for(var i = 0; i < ; i++) {
  // traverse each cookie   var cookie = list[i];
   var p = ("=");
   // Find the first "=" symbol   var name = (0,p);
   // Get the name of the cookie   var value = (p+1);
   // Get the value corresponding to the cookie   value = decodeURIComponent(value);
   // Decode its value   cookies[name] = value;
   //Storing name-value pairs into objects  }
  return cookies;
 }());
 //Storing all cookies' names into an array var keys = [];
 for(var key in cookies) (key);
 // Now define the properties and methods that store API public // The number of cookies stored  = ;
 // Return the name of the nth cookie, if n goes out of bounds, return null  = function(n) {
  if (n < 0 || n >= ) return null;
  return keys[n];
 };
 // Returns the cookie value of the specified name, and if it does not exist, it returns null  = function(name){
 return cookies[name] || null;
 };
 //Storage cookie values  = function(key, value) {
  if (!(key in cookies)) {
  // If the cookie to be facilitated does not exist yet   (key);
   // Add the specified name to the array that stores all cookie names   ++;
   // Add one cookies  }
  //Storage the name/value pair data into the cookie object.  cookies[key] = value;
  // Start setting cookies officially.  // First encode the value of the cookie to be stored  // Create a string in the form of "name=encoded value" at the same time  var cookie = key + "=" + encodeURIComponent(value);
  // Add the cookie attribute to the string  if (maxage) cookie += "; max-age=" + maxage;
  if (path) cookie += "; path=" + path;
  // Set cookies through attributes   = cookie;
 };
 // Delete the specified cookie  = function(key) {
  if (!(key in cookies)) return;
  // If the cookie does not exist, do nothing  // Remove specified cookies from the cookies group maintained internally  delete cookies[key];
  // At the same time, delete the name in the cookie in the internal array.  // It will be easier if you use the array indexOf() method defined by ES5.  for(var i = 0; i < ; i++) {
  // traverse all names   if (keys[i] === key) { 
   // When we found the one we were looking for    (i,1); 
 // Remove it from the array.    break;
   }
  }
  --; 
  // minus one cookies  // Finally, by setting the value of the cookie to an empty string  // and set the validity period to 0 to delete the specified cookie.   = key + "=; max-age=0";
 };
 // Delete all cookies  = function() {
  // Loop all cookies' names and delete cookies  for(var i = 0; i < ; i++)
    = keys[i] + "=; max-age=0";
  // Reset all internal states  cookies = {};
  keys = [];
   = 0;
 };
}

I hope this article will be helpful to everyone's JavaScript programming.