1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
function setCookie(cookieName, cookieValue, expires) { // 设置Cookie function getCookieName(cookieName) { //获取并返回与 cookieName 同名的 cookie 名称,允许大小写不同,如果不存在这样的 cookie,就返回 cookieName var lowerCookieName = cookieName.toLowerCase(); //转换为小写 var cookieStr = document.cookie; // 获取当前cookie if (cookieStr == "") { return cookieName; } var cookieArr = cookieStr.split(";"); var pos = -1; for (var i = 0; i < cookieStr.length; i++) { pos = cookieArr[i].indexOf("="); if (pos > 0) { if (cookieArr[i].substring(0, pos).toLowerCase() == lowerCookieName) { return cookieArr[i].substring(0, pos); } } } return cookieName; } function writeCookie(cookieName, cookieValue, expires) { // 写cookie,不区分大小写 if (expires) { document.cookie = getCookieName(cookieName) + "=" + escape(cookieValue) + "; expires=" + expires.toGMTString(); } else { document.cookie = getCookieName(cookieName) + "=" + escape(cookieValue); } } function readCookie(cookieName, defaultValue) { //获取并返回 cookie 值,不区分大小写 var lowerCookieName = cookieName.toLowerCase(); var cookieStr = document.cookie; if (cookieStr == "") { return defaultValue; } var cookieArr = cookieStr.split("; "); var pos = -1; for (var i = 0; i < cookieArr.length; i++) { pos = cookieArr[i].indexOf("="); if (pos > 0) { if (cookieArr[i].substring(0, pos).toLowerCase() == lowerCookieName) { return unescape(cookieArr[i].substring(pos + 1, cookieArr[i].length)); } } } return defaultValue; } writeCookie(cookieName, cookieValue, expires); } |