js操作cookie
JavaScript处理cookie
//取cookie值 getCookie: function(cname) { var allcookies = document.cookie; //索引长度,开始索引的位置 var cookie_pos = allcookies.indexOf(cname); // 如果找到了索引,就代表cookie存在,否则不存在 if (cookie_pos != -1) { // 把cookie_pos放在值的开始,只要给值加1即可 //计算取cookie值得开始索引,加的1为“=” cookie_pos = cookie_pos + cname.length + 1; //计算取cookie值得结束索引 var cookie_end = allcookies.indexOf(";", cookie_pos); if (cookie_end == -1) { cookie_end = allcookies.length; } //得到想要的cookie的值 var value = unescape(allcookies.substring(cookie_pos, cookie_end)); } return value; }, //设置cookie值 setCookie: function(cname, cvalue, exdays) { if (!exdays) { exdays = 1; } var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toUTCString(); //console.info(cname + "=" + cvalue + "; " + expires); document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/"; //console.info(document.cookie); }, // 清除Cookies clearCookies: function(cname) { if (!cname) { //清除所有 var keys = document.cookie.match(/[^ =;]+(?=\=)/g); if (keys) { for (var i = keys.length; i--;) { document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() + "; path=/"; } } } else { //清除单个时 common.setCookie(cname, '', -1); } },