var CookieUtil = { //依据名称取得cookie值 例如 CookieUtil.set("data", "name=nic&book=pageXOffset%20js"); alert(CookieUtil.get("data")); get: function (name) { var cookieName = encodeURIComponent(name) + "=", cookieStart = document.cookie.indexOf(cookieName),//document.cookie的属性 cookieValue = null; if (cookieStart > -1) { var cookieEnd = document.cookie.indexOf(";", cookieStart); if (cookieEnd == -1) { cookieEnd = document.cookie.length; } cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd)); } return cookieValue; }, //Set-Cookie:name=value;expires:Mon,22-Jan-07 07:10:24 GMT;domain=.wrox.com;path=/;secure //设置cookie set: function (name,value,expires,path,domain,secure) { var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value); if (expires instanceof Date) { cookieText += ";expires=" + expires.toGMTString(); } if (path) { cookieText += ";path=" + path; } if (domain) { cookieText += ";domain=" + domain; } if (secure) {//使用ssl cookieText += ";secure"; } document.cookie = cookieText; }, //取消cookie(使cookie值过期) unset: function (name, path, domain, secure) { this.set(name, "", new Date(0), path, domain, secure); } } //子cookie使用cookie值来存储多个名称值对形如name=name1=value1&name2=value2&name3=value3 //例如 var data = SubCookieUtil.getAll("data"); //alert(data.name); //alert(data.book); var SubCookieUtil = { //取得cookie值及其子cookie get: function (name, subName) { var subCookies = this.getAll(name); if (subCookies) return subCookies[subName]; else return null; }, //取到全部子cookie对象 getAll: function (name) { var cookieName = encodeURIComponent(name) + "=", cookieStart = document.cookie.indexOf(cookieName), cookieValue = null, cookieEnd, subCookies, i, parts, result = {}; if (cookieStart > -1) { cookieEnd = document.cookie.indexOf(";", cookieStart); if (cookieEnd == -1) { cookieEnd = document.cookie.length; } cookieValue = document.cookie.substring(cookieStart + cookieName.length, cookieEnd); if (cookieValue.length > 0) { subCookies = decodeURIComponent(cookieValue).split("&"); for (i = 0, len = subCookies.length; i < len; i++) { parts = subCookies[i].split("="); result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]); } return result; } } return null; }, //设置子cookie set: function (name,subName,value,expires,path,domain,secure) { var subcookies = this.getAll(name) || {}; subcookies[subName] = value; this.setAll(name,subcookies,expires,path,domain,secure); }, setAll: function (name,subcookies,expires,path,domain,secure) { var cookieText = encodeURIComponent(name) + "=", subcookieParts = new Array(), subName; for (subName in subcookies) { if (subName.length > 0 && subcookies.hasOwnProperty(subName)) { subcookieParts.push(encodeURIComponent(subName) + "=" + encodeURIComponent(subcookies[subName])); } } if (cookieParts.length > 0) { cookieText += subcookieParts.join("&"); if (expires instanceof Date) { cookieText += ";expires=" + expires.toGMTString(); } if (path) { cookieText += ";path=" + path; } if (domain) { cookieText += ";domain=" + domain; } if (secure) { cookieText == ";secure"; } } else { cookieText += ";expires=" + (new Date(0)).toGMTString(); } document.cookie = cookieText; }, //注销子cookie unset: function (name,subName,path,domain,secure) { var subcookies = this.getAll(name); if (subcookies) { delete subcookies[subName]; this.setAll(name, subcookies, null, path, domain, secure); } }, unsetAll: function (name, path, domain, secure) { this.setAll(name, null, new Date(0), path, domain, secure); } }
时间: 2024-10-20 23:21:21