쿠키 패키지

3131 단어
js 버 전
var cookie = {
    //  key      cookie
    get:function(key){
        //  cookie
        var data = document.cookie;
        //  key            pwd=
        var startIndex = data.indexOf(key + '=');
        //  name=123;pwd=abc
        //         0   cookie
        if(startIndex > -1) {
            //key             key   + 1
            startIndex = startIndex + key.length + 1;
            //       key          ;       
            var endIndex = data.indexOf(';',startIndex);
            //                cookie  ,         
            endIndex = endIndex < 0 ? data.length : endIndex;
            return decodeURIComponent(data.substring(startIndex,endIndex));
        }else {
            return '';
        }
    },

    set:function(key,value,time){
        //      
        var time = time;
        //      
        var cur = new Date();
        var undefined;
        //      
        cur.setTime(cur.getTime() + time*24*3600*1000);
        //  cookie           GMT  
        document.cookie = key + '=' + encodeURIComponent(value)+ ';expires = ' + (time===undefined ? '' : cur.toGMTString());
    },

    del:function(key){
        //  cookie
        var data = this.get(key);
        //     cookie     cookie          
        if(data!==false){
            this.set(key,data,-1);
        }
    }
};

//  
        //  cookies
        function setCookie(name, value) {
            var days = 1;
            var exp = new Date();
            exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
            document.cookie = name + '=' + encodeURI(value) + ';expires=' + exp.toUTCString();
        }
        //  cookies
        function getCookie(name) {
            var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
            if (arr = document.cookie.match(reg))
                return decodeURI(arr[2]);
            return null;
        }
        //  cookies 
        function delCookie(name) {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval = getCookie(name);
            if (!!cval)
                document.cookie = name + '=' + cval + ';expires=' + exp.toUTCString();
        }

angualr 버 전
  //  cookies
  public setCookie(name: string, value: string): void {
    let Days = 1;
    const exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + '=' + encodeURI(value) + ';expires=' + exp.toUTCString();
  }
  //  cookies
  public getCookie(name: string) {
    let arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
    if (arr = document.cookie.match(reg))
      return decodeURI(arr[2]);
    else
      return null;
  }
  //  cookies 
  public delCookie(name: string) {
    const exp = new Date();
    exp.setTime(exp.getTime() - 1);
    const cval = this.getCookie(name);
    if (cval != null)
      document.cookie = name + '=' + cval + ';expires=' + exp.toUTCString();
  }

좋은 웹페이지 즐겨찾기