js存取cookie以及设置cookie储存时间

// 设置cookie expires储存时间按秒计算
    function setUserCookie(name, value, expires) {
        var date = new Date();
        date.setTime(date.getTime() + (expires * 1000));
        document.cookie = name + "="+ escape (value) + ";expires=" + date.toGMTString();
    }
  
    // 获取cookie'.
    function getUserCookie(name){
        var strcookie = document.cookie;//获取cookie字符串
        var arrcookie = strcookie.split("; ");//分割
        //遍历匹配
        for ( var i = 0; i < arrcookie.length; i++) {
            var arr = arrcookie[i].split("=");
            if (arr[0] == name){
                return arr[1];
            }
        }
        return "";
    }

你可能感兴趣的:(JavaScript,javascript,前端,开发语言)