前端开发工具篇

JavaScript操作本地Cookie

2018-11-05  本文已影响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 : "");
     }
上一篇下一篇

猜你喜欢

热点阅读