JavaScript sets gets and clears methods for single valued and multi valued cookie

  • 2020-11-18 05:14:59
  • OfStack

Without further ado, I will post the code directly to you.

The specific code is as follows:


var CookieUtil = (function () {
   var Cookie = function () {
     //  To obtain a single value cookie
     this.get = function(name) {
       var start = document.cookie.indexOf(encodeURIComponent(name)) ;
       var end = document.cookie.indexOf(';', start) ;
       if(end == -) {
         end = document.cookie.length;
       }
       return decodeURIComponent(document.cookie.substring(start+name.length+,end));
     };
     //  Set the single value cookie
     this.set = function(name, value, expires, path, domain, secure) {
       var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
       //  Set the default expiration time to 7 day 
       if(expires == undefined) {
         var date = new Date();
         date.setTime(date.getTime() + ****);
         expires = date ;
       }
       if(expires instanceof Date) {
         cookieText += "; expires=" + expires.toGMTString();
       }
       if(path != undefined) {
         cookieText += "; path=" + path;
       }
       if(domain != undefined) {
         cookieText += "; domain" + domain;
       }
       if(secure != undefined) {
         cookieText += "; secure";
       }
       document.cookie = cookieText;
     };
     //  Remove a single value cookie
     this.unset = function(name, path, domain, secure) {
       this.set(name, '', new Date(), path, domain, secure );
     };
     //  Set up more value cookie
     this.setAll = function(name, subCookies, expires, path, domain, secure) {
       var cookieText = ";" + encodeURIComponent(name) + "=",
       arr = new Array();
       for(var attr in subCookies) {
         arr.push([encodeURIComponent(attr)] + ":" + encodeURIComponent(subCookies[attr]));
       } 
       this.set(name, arr.join('&'), expires, path, domain, secure);
     };
     //  To get more value cookie
     this.getAll = function(name) {
       var obj = {};
       var arr = this.get(name).split('&');
       for(var i = , len = arr.length; i < len; i++) {
         var tmpArr = arr[i].split(':');
         obj[decodeURIComponent(tmpArr[])] = decodeURIComponent(tmpArr[]);
       }
       return obj;
     };
     //  To get more value cookie The son of cookie
     this.getSub = function(name, subname) {
       var obj = this.getAll(name);
       return obj[subname];
     };
     //  Clears the specified multiple values cookie
     this.unsetAll = function(name,path,domain,secure) {
       this.unset(name, '', new Date(), path, domain, secure);
     };
     //  Clears the specified multiple values cookie The son of cookie
     this.unsetSub = function(name, subname,path, domain, secure) {
       var obj = this.getAll(name);
       delete obj[subname];
       this.setAll(name, obj, null, path, domain, secure);
     };
   };
   return new Cookie();
 })();

The above code is this article to introduce JavaScript setting, get, clear single value and multi-value cookie method, any unclear place welcome to leave a message to me.


Related articles: