美文网首页
在请求头里存储cookie

在请求头里存储cookie

作者: 西西_80ff | 来源:发表于2018-06-13 17:32 被阅读0次

    使用:

    cookieUtil.set(key,value,' ', '/');

    引入jquery.cookie.js

    var cookieUtil={

        set :function(name,value,expires,path,domain,secure){

            var cookieText=encodeURIComponent(name)+'='+

                encodeURIComponent(value);

            if(expires instanceof Date){

                cookieText+=';expires='+expires.toGMTString();

            }

            if(path){

                cookieText+=';path='+path;

            }

            if(domain){

                cookieText+=';domain='+domain;

            }

            if(secure){

                cookieText+=';secure';

            }

            document.cookie=cookieText;

        },

        get:function(name){

            var cookieName=encodeURIComponent(name)+'=',

                cookieStart=document.cookie.indexOf(cookieName),

                cookieValue=null;

            if(cookieStart>-1){

                var cookieEnd=document.cookie.indexOf(';',cookieStart);

                if(cookieEnd==-1){

                    cookieEnd=document.cookie.length;

                }

                cookieValue=decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length,cookieEnd))

            }

            return cookieValue;

        },

        unset:function(name,path,domain,secure){

            this.set(name,'',new Date(0),domain,path)

        }

    }

    function setCookieDate(day){

        var date=null;

        if(typeof day=='number'&&day>0){

            date=new Date();

            date.setDate(date.getDate()+day);

        }else{

            throw new Error('!!')

        }

        return date;

    }

    function getUUID() {

        return 'xxxx-xxxx-xxxx-xxxx-xxxx'.replace(/[xy]/g, function(c) {

            var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);

            return v.toString(16);

        });

    };

    相关文章

      网友评论

          本文标题:在请求头里存储cookie

          本文链接:https://www.haomeiwen.com/subject/jifseftx.html