免除单值和多值cookie的艺术,JS获取后台Cookies值

2019-09-03 18:02 来源:未知

废话不多说了,直接给大家贴代码了。

复制代码 代码如下:

具体代码如下:

function getCookie(name)
    {   
        var offset,cookieValue;
        var search=name+"=";
        if(document.cookie.length>0)
        {
            offset=document.cookie.indexOf(search);
            if(offset!=-1)
            {
                offset += search.length;  
                end = document.cookie.indexOf(";", offset);  
                if (end == -1)
                    end = document.cookie.length;  
                cookieValue=unescape(document.cookie.substring(offset, end));
            }
        }
        return cookieValue;
    }
//name表示cookie的键的名字

var CookieUtil = (function () {
   var Cookie = function () {
     // 获取单值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));
     };
     // 设置单值cookie
     this.set = function(name, value, expires, path, domain, secure) {
       var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
       // 设置默认过期时间为七天
       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;
     };
     // 清除单值cookie
     this.unset = function(name, path, domain, secure) {
       this.set(name, '', new Date(), path, domain, secure );
     };
     // 设置多值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);
     };
     // 获取多值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;
     };
     // 获取多值cookie的子cookie
     this.getSub = function(name, subname) {
       var obj = this.getAll(name);
       return obj[subname];
     };
     // 清除指定的多值cookie
     this.unsetAll = function(name,path,domain,secure) {
       this.unset(name, '', new Date(), path, domain, secure);
     };
     // 清除指定多值cookie的子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();
 })();

您可能感兴趣的文章:

  • javascript js cookie的存储,获取和删除
  • JS获取dom 对象 ajax操作 读写cookie函数
  • ASP.NET获取不到JS设置cookies的解决方法
  • JS设置获取cookies的方法
  • js获取指定的cookie的具体实现
  • JavaScript设置、获取、清除单值和多值cookie的方法

 以上代码就是本文给大家介绍JavaScript设置、获取、清除单值和多值cookie的方法,有哪里不清楚的地方欢迎给我留言。

您可能感兴趣的文章:

  • javascript js cookie的存储,获取和删除
  • JS获取dom 对象 ajax操作 读写cookie函数
  • ASP.NET获取不到JS设置cookies的解决方法
  • JS获取后台Cookies值的小例子
  • JS设置获取cookies的方法
  • js获取指定的cookie的具体实现
TAG标签:
版权声明:本文由990888藏宝阁发布于计算机网络,转载请注明出处:免除单值和多值cookie的艺术,JS获取后台Cookies值