美文网首页前端开发工具篇
JavaScript操作本地Cookie

JavaScript操作本地Cookie

作者: 无为无味无心 | 来源:发表于2018-11-05 10:45 被阅读107次
     function getCookie(cName) {
            var cookieString = decodeURI(document.cookie);
            var cookieArray = cookieString.split("; ");
            for (var i = 0; i < cookieArray.length; i++) {
                var cookieNum = cookieArray[i].split("=");
                var cookieName = cookieNum[0];
                var cookieValue = cookieNum[1];
                if (cookieName == cName) {
                    return cookieNum;
                }
            }
            return false;
        }
        function deleteCookie(key) {
            var date = new Date();
            date.setTime(date.getTime() - 1000);
            setCookie(key, "", date.toGMTString(), "", "", "");
        }
    
         function setCookie(name, value, expires, path, domain, secure) {
             document.cookie = name + "=" + encodeURI(value) +
             ((expires) ? "; expires=" + expires : "") +
             ((path) ? "; path=" + path : "") +
             ((domain) ? "; domain=" + domain : "") +
             ((secure) ? "; secure=" + secure : "");
         }
    

    相关文章

      网友评论

        本文标题:JavaScript操作本地Cookie

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