cookie and subcookie utilty in javascript

//cookie util

 1  var CookieUtil = {
 2 
 3         get: function (name) {
 4             var cookieName = encodeURIComponent(name) + "=",
 5                  cookieStart = document.cookie.indexOf(cookieName),
 6                  cookieValue = null;
 7 
 8             if (cookieStart > -1) {
 9                 var cookieEnd = document.cookie.indexOf(";", cookieStart);
10                 if (cookieEnd == -1) {
11                     cookieEnd = document.cookie.length;
12                 }
13                 cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
14             }
15             return cookieValue;
16         },
17 
18         set: function (name, value, expires, path, domain, secure) {
19             var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
20 
21             if (expires instanceof Date) {
22                 cookieText += "; expires=" + expires.toGMTString();
23             }
24             if (path) {
25                 cookieText += "; path=" + path;
26             }
27             if (domain) {
28                 cookieText += "; domain=" + domain;
29             }
30             if (secure) {
31                 cookieText += "; secure";
32             }
33             document.cookie = cookieText;
34         },
35 
36         unset: function (name, path, domain, secure) {
37             this.set(name, "", new Date(0), path, domain, secure);
38         }
39     };

//subcookie util

 1 var SubCookieUtil = {
 2 
 3         get: function (name, subName) {
 4             var subCookies = this.getAll(name);
 5             if (subCookies) {
 6                 return subCookies[subName];
 7             } else {
 8                 return null;
 9             }
10         },
11 
12         getAll: function (name) {
13             var cookieName = encodeURIComponent(name) + "=",
14                 cookieStart = document.cookie.indexOf(cookieName),
15                 cookieValue = null,
16                 result = {};
17 
18             if (cookieStart > -1) {
19                 var cookieEnd = document.cookie.indexOf(";", cookieStart);
20                 if (cookieEnd == -1) {
21                     cookieEnd = document.cookie.length;
22                 }
23                 cookieValue = document.cookie.substring(cookieStart + cookieName.length, cookieEnd);
24 
25                 if (cookieValue.length > 0) {
26                     var subCookies = cookieValue.split("&");
27 
28                     for (var i = 0, len = subCookies.length; i < len; i++) {
29                         var parts = subCookies[i].split("=");
30                         result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
31                     }
32                     return result;
33                 }
34             }
35             return null;
36         },
37 
38         set: function (name, subName, value, expires, path, domain, secure) {
39             var subcookies = this.getAll(name) || {};
40             subcookies[subName] = value;
41             this.setAll(name, subcookies, expires, path, domain, secure);
42         },
43 
44         setAll: function (name, subcookies, expires, path, domain, secure) {
45 
46             var cookieText = encodeURIComponent(name) + "=";
47             var subcookieParts = new Array();
48 
49             for (var subName in subcookies) {
50                 if (subName.length > 0 && subcookies.hasOwnProperty(subName)) {
51                     subcookieParts.push(encodeURIComponent(subName) + "=" + encodeURIComponent(subcookies[subName]));
52                 }
53             }
54 
55             if (subcookieParts.length > 0) {
56                 cookieText += subcookieParts.join("&");
57 
58                 if (expires instanceof Date) {
59                     cookieText += "; expires=" + expires.toGMTString();
60                 }
61 
62                 if (path) {
63                     cookieText += "; path=" + path;
64                 }
65 
66                 if (domain) {
67                     cookieText += ";domain=" + domain;
68                 }
69 
70                 if (secure) {
71                     cookieText += ";secure";
72                 }
73             } else {
74                 cookieText += "; expires=" + (new Date(0)).toGMTString();
75             }
76             document.cookie = cookieText;
77         },
78 
79         unset: function (name, subName, path, domain, secure) {
80             var subcookies = this.getAll(name);
81             if (subcookies) {
82                 delete subcookies[subName];
83                 this.setAll(name, subcookies, null, path, domain, secure);
84             }
85         },
86 
87         unsetAll: function (name, path, domain, secure) {
88             this.setAll(name, null, new Date(0), path, domain, secure);
89         }
90     };

 

posted on 2013-05-19 12:45  雷音  阅读(197)  评论(0编辑  收藏  举报

导航