1 /**2 * 封装cookie的操作3 * @type {Object}4 */5 var CookieUtil = {6 /**7 * 根据cookie的名字获取相应的值8 * @param name cookie名字9 * @return {*}10 */11 get:function (name) {12 //对name进行URL编码13 var cookieName = encodeURIComponent(name) + '=',14 cookieStart = document.cookie.indexOf(cookieName),15 cookieValue = null;16 //找到cookieName17 if (cookieStart > -1) {18 //以cookieStart为起点找到最近的";"19 var cookieEnd = document.cookie.indexOf(';', cookieStart);20 //没找到";",则是document.cookie的最后一个值21 if (cookieEnd === -1) {22 cookieEnd = document.cookie.length;23 }24 //提取相应value字段25 cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));26 }27 //返回28 return cookieValue;29 },30 /**31 * 设置一个cookie32 * @param name cookie名字33 * @param value 相应的值34 * @param expire 生存周期 Date35 * @param path 路径36 * @param domain 域名37 * @param secure Boolean38 */39 set:function (name, value, expire, path, domain, secure) {40 //必须先进行URL编码41 var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value);42 if (expire instanceof Date) {43 //toGMTString()方法正确格式化Date对象44 cookieText += '; expire=' + expire.toGMTString();45 }46 if (path) {47 cookieText += '; path=' + path;48 }49 if (domain) {50 cookieText += '; domain=' + domain;51 }52 if (secure) {53 cookieText += '; secure';54 }55 document.cookie = cookieText;56 },57 /**58 * 删除cookie59 * @param name60 * @param path61 * @param domain62 * @param secure63 */64 unset:function (name, path, domain, secure) {65 this.set(name, '', new Date(0), path, domain, secure);66 }67 };68 /*69 //set cookies70 CookieUtil.set(“name”, “Nicholas”);71 CookieUtil.set(“book”, “Professional JavaScript”);72 //read the values73 alert(CookieUtil.get(“name”)); //”Nicholas”74 alert(CookieUtil.get(“book”)); //”Professional JavaScript”75 //remove the cookies76 CookieUtil.unset(“name”);77 CookieUtil.unset(“book”);78 //set a cookie with path, domain, and expiration date79 CookieUtil.set(“name”, “Nicholas”, “/books/projs/”, “www.wrox.com”,80 new Date(“January 1, 2010”));81 //delete that same cookie82 CookieUtil.unset(“name”, “/books/projs/”, “www.wrox.com”);83 //set a secure cookie84 CookieUtil.set(“name”, “Nicholas”, null, null, null, true);85 */86 87 //子cookie的操作88 //为了绕开浏览器的单域名下的cookie数限制89 //子cookie是存放在单个cookie中的更小段的数据90 var SubCookieUtil = {91 /**92 * 获取单个子cookie的值93 * @param name cookie名称94 * @param subName 子cookie名称95 * @return {*}96 */97 get:function (name, subName) {98 var subCookies = this.getAll(name);99 if (subCookies) {
100 return subCookies(subName);
101 } else {
102 return null;
103 }
104 },
105 /**
106 * 获取所有子cookie并将它们放入一个对象中返回
107 * @param name cookie名称
108 * @return {*} 返回对象或null
109 */
110 getAll:function (name) {
111 var cookieName = encodeURIComponent(name) + "=",
112 cookieStart = document.cookie.indexOf(cookieName),
113 cookieValue = null,
114 result = {};
115 if (cookieStart > -1) {
116 var cookieEnd = document.cookie.indexOf(";", cookieName);
117 if (cookieEnd === -1) {
118 cookieEnd = document.cookie.length;
119 }
120 //没有进行解码,因为要对子cookie分离操作
121 cookieValue = document.cookie.substring(cookieStart + cookieName.length, cookieEnd);
122 //
123 if (cookieValue.length > 0) {
124 //分离出子cookie的对象
125 var subCookies = cookieValue.split("&");
126 //遍历分隔出子cookie的名称和值,解码后返回对象
127 for (var i = 0, len = subCookies.length; i < len; i++) {
128 var parts = subCookies[i].split("=");
129 result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
130 }
131 return result;
132 }
133 }
134 //没找到,返回空
135 return null;
136 },
137 /**
138 * 存储单个子cookie
139 * @param name cookie名称
140 * @param subName 子cookie名称
141 * @param value 子cookie值
142 * @param expires 失效日期
143 * @param path 路径
144 * @param domain 域名
145 * @param secure Boolean安全作用域
146 */
147 set:function (name, subName, value, expires, path, domain, secure) {
148 //获取name名称的所有子cookie,没有则创建空对象
149 var subcookies = this.getAll(name) || {};
150 //给对象添加属性和值
151 subcookies[subName] = value;
152 //调用setAll()方法
153 this.setAll(name, subcookies, expires, path, domain, secure);
154 },
155 /**
156 * 存储所有子cookie
157 * @param name cookie名称
158 * @param subcookies 子cookie名称
159 * @param expires 失效日期
160 * @param path 路径
161 * @param domain 域名
162 * @param secure Boolean安全作用域
163 */
164 setAll:function (name, subcookies, expires, path, domain, secure) {
165 var cookieText = encodeURIComponent(name) + "=";
166 //创建数组,用于保存子cookie
167 var subcookieParts = [];
168 //遍历对象属性
169 for (var subName in subcookies) {
170 //如果存在,则把编码后的名称和值保存到数组
171 if (subName.length > 0 && subcookies.hasOwnProperty(subName)) {
172 subcookieParts.push(encodeURIComponent(subName) + "=" + encodeURIComponent(subcookies[subName]));
173 }
174 }
175 //存在子cookie
176 if (subcookieParts.length > 0) {
177 //连接子cookie
178 cookieText += subcookieParts.join("& ");
179 if (expires instanceof Date) {
180 cookieText += ";expires=" + expires.toGMTString();
181 }
182 if (path) {
183 cookieText += ";path=" + path;
184 }
185 if (domain) {
186 cookieText += ";domain=" + domain;
187 }
188 if (secure) {
189 cookieText += ";secure";
190 }
191 } else {
192 //相当于删除cookie操作
193 cookieText += ";expires=" + (new Date(0)).toGMTString();
194 }
195 document.cookie = cookieText;
196 },
197 /**
198 * 删除单个子cookie的名称和值
199 * @param name
200 * @param subName
201 * @param path
202 * @param domain
203 * @param secure
204 */
205 unset:function (name, subName, path, domain, secure) {
206 var subcookies = this.getAll(name);
207 if (subcookies) {
208 //删除对应的属性和值
209 delete subcookies[subName];
210 //重新设置cookie
211 this.setAll(name, subcookies, null, path, domain, secure);
212 }
213 },
214 /**
215 * 删除所有子cookie
216 * @param name
217 * @param path
218 * @param domain
219 * @param secure
220 */
221 unsetAll:function (name, path, domain, secure) {
222 this.setAll(name, null, new Date(0), path, domain, secure);
223 }
224 };
225 /*
226 //assume document.cookie=data=name=Nicholas&book=Professional%20JavaScript
227 //get all subcookies
228 var data = SubCookieUtil.getAll(“data”);
229 alert(data.name); //”Nicholas”
230 alert(data.book); //”Professional JavaScript”
231 //get subcookies individually
232 alert(SubCookieUtil.get(“data”, “name”)); //”Nicholas”
233 alert(SubCookieUtil.get(“data”, “book”)); //”Professional JavaScript”
234
235 //assume document.cookie=data=name=Nicholas&book=Professional%20JavaScript
236 //set two subcookies
237 SubCookieUtil.set(“data”, “name”, “Nicholas”);
238 SubCookieUtil.set(“data”, “book”, “Professional JavaScript”);
239 //set all subcookies with expiration date
240 SubCookieUtil.setAll(“data”, { name: “Nicholas”, book: “Professional JavaScript” },
241 new Date(“January 1, 2010”));
242 //change the value of name and change expiration date for cookie
243 SubCookieUtil.set(“data”, “name”, “Michael”, new Date(“February 1, 2010”));
244
245 //just remove the “name” subcookie
246 SubCookieUtil.unset(“data”, “name”);
247 //remove the entire cookie
248 SubCookieUtil.unsetAll(“data”);
249 */
转载于:https://www.cnblogs.com/webFrontDev/archive/2012/11/24/2786022.html