美文网首页
封装cookie

封装cookie

作者: Hello杨先生 | 来源:发表于2019-07-20 23:11 被阅读0次
    // document.cookie = "name='张三';expires=time"
        function setCookie(name, value, time) {
            var now = new Date()
            now.setDate(now.getDate()+time)//设置过期时间
            document.cookie = name+'='+value+';expires='+now
        }
    
        function getCookie(name) {
            var arr = document.cookie.split(';')
            for (var i=0;i<arr.length;i++){
                var arr2 = arr[i].split('=')
                if(arr2[0] == name){
                    return arr2[1]
                }
            }
            return ''
        }
    
        function removeCookie(name){
            setCookie(name,11231321,-1)
        }
    
    
        setCookie("stu","张三",1)
        setCookie("stu1","张三1",1)
            // ['stu=张三', 'stu1=张三1']
            // ['stu','张三']
        // var cook = document.cookie
    

    相关文章

      网友评论

          本文标题:封装cookie

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