WEB开发网
开发学院网页设计JavaScript 使用JavaScript 对Cookie 操作的封装 阅读

使用JavaScript 对Cookie 操作的封装

 2010-01-08 00:00:00 来源:WEB开发网   
核心提示: 而在平常的开发中,如果仅仅使用 document.cookie 属性进行访问,会很麻烦,因为只能向其赋值字符串,并且在读取后还要进行字符串切割,才能获取指定变量名称的值.document.cookie 读取时,返回的是所有赋值的值,而不包括过期时间,域之类的信息,只能再次独设置.下面就附上代码,

而在平常的开发中,如果仅仅使用 document.cookie 属性进行访问,会很麻烦,因为只能向其赋值字符串,并且在读取后还要进行字符串切割,才能获取指定变量名称的值.document.cookie 读取时,返回的是所有赋值的值,而不包括过期时间,域之类的信息,只能再次独设置.

下面就附上代码,全部封装到Cookie全局对象中,暴露出几个方法.

Get : 返回指定所有cookie字符串.

Set : 设置cookie 字符串.

Clear : 清除所有cookie对象.

GetDayTime : 获取指定距今val天的Date对象.

Write : 写cookie.已重载.详见代码.

Query : 查询cookie. 已重载.详见代码.

Update : 修改cookie.

Delete : 删除cookie.

代码1-7

  1 Cookie = {};
  2 /*
  3 *       Date对象的setTime方法是设置距离1970-01-01以来的毫秒数,设置到对象里去,返回的是据那以后的毫秒数而不是原对象.
  4 *       如果Cookie 不设置 expires 属性,或者expires 时间比本地时间少,那么将会在下一次浏览时过期.
  5 *       document.cookie 对象返回的是所有值的字符串形式,不包含 expires 或者其他.
  6 *
  7 */
  8 (function() {
  9     var nDay = 24 * 60 * 60 * 1000;
 10     var isString = function(v) {
 11         return typeof v === "string";
 12     }
 13     var isArray = function(v) {
 14         return Object.prototype.toString.apply(v) == "[object Array]";
 15     }
 16     var isObject = function(v) {
 17         return v && typeof v == "object";
 18     }
 19     var isDate = function(v) {
 20         return Object.prototype.toString.apply(v) == "[object Date]";
 21     }
 22     var getTime = function() {
 23         return new Date().getTime();
 24     }
 25     var trim = function(val) {
 26         return (val || "").replace(/^\s+|\s+$/g, "");
 27     }
 28     var tryEval = function(val) {
 29         var Obj, e;
 30         try {
 31             Obj = eval(val);
 32         } catch (e) {
 33             Obj = val;
 34         }
 35         return Obj;
 36     }
 37     var ObjectToString = function(o) {
 38         var tstr = "{";
 39         for (var v in o) {
 40             if (isArray(o[v])) {
 41                 tstr += v + ":" + ArrayToString(o[v]) + ",";
 42             } else if (isObject(o[v])) {
 43                 tstr += v + ":" + ObjectToString(o[v]) + ",";
 44             } else if (isString(o[v])) {
 45                 tstr += v + ":\"" + o[v].toString() + "\",";
 46             } else {
 47                 tstr += v + ":" + o[v].toString() + ",";
 48             }
 49         }
 50         return tstr.slice(0, -1) + "}";
 51     }
 52     var ArrayToString = function(o) {
 53         var tstr = "[";
 54         for (var v in o) {
 55             if (isArray(o[v])) {
 56                 tstr += ArrayToString(o[v]) + ",";
 57             } else if (isObject(o[v])) {
 58                 tstr += ObjectToString(o[v]) + ",";
 59             } else {
 60                 tstr += o[v].toString() + ",";
 61             }
 62         }
 63         return tstr.slice(0, -1) + "]"; ;
 64     }
 65     Cookie = {
 66         Get: function() {
 67             return document.cookie;
 68         },
 69         Set: function(val) {
 70             document.cookie = val;
 71         },
 72         Clear: function() {
 73             var temp = this.Query();
 74             var o;
 75             for (o in temp) {
 76                 this.Delete(o);
 77             }
 78         },
 79         GetDayTime: function(val) {
 80             var texpires = new Date();
 81             texpires.setTime(texpires.getTime() + val * nDay);
 82             return texpires;
 83         },
 84         Write: function() {
 85             /*
 86             *   Cookie.Write(Object); 写入对象,名称为main;
 87             *   Cookie.Write(varname, Object); varname:变量名, Object:写入对象;
 88             *   Cookie.Write(Object, Date); Object:写入对象, Date:过期时间;
 89             *   Cookie.Write(varname, Object, Date); varname:变量名, Object:写入对象, Date:过期时间;
 90             *   Cookie.Write(varname, Object, Date, Domain, Path); varname:变量名, Object:写入对象, Date:过期时间, Domain:域, Path: 子目录;
 91             */
 92             if (arguments.length == 1) {
 93                 var tvalue = arguments[0];
 94                 var tstr = "";
 95                 var texpires = new Date(); texpires.setTime(texpires.getTime() + 1 * nDay);
 96                 if (isArray(tvalue)) {
 97                     tstr = ArrayToString(tvalue);
 98                 } else if (isObject(tvalue)) {
 99                     tstr = ObjectToString(tvalue);
100                 } else {
101                     tstr = tvalue.toString();
102                 }
103                 tstr = "main=" + escape(tstr) + ";expires=" + texpires.toGMTString() + ";";
104             } else if (arguments.length == 2) {
105                 var tname, tvalue, texpires, tstr = "";
106                 if (isDate(arguments[1])) {
107                     tname = "main";
108                     tvalue = arguments[0];
109                     texpires = arguments[1];
110                 } else {
111                     tname = arguments[0];
112                     tvalue = arguments[1];
113                     texpires = new Date(); texpires.setTime(texpires.getTime() + 1 * nDay);
114                 }
115 
116                 if (isArray(tvalue)) {
117                     tstr += ArrayToString(tvalue);
118                 } else if (isObject(tvalue)) {
119                     tstr += ObjectToString(tvalue);
120                 } else {
121                     tstr = tvalue.toString();
122                 }
123                 tstr = tname + "=" + escape(tvalue) + ";expires=" + texpires.toGMTString() + ";";
124 
125             } else if (arguments.length == 3) {
126                 var tname = arguments[0], tvalue = arguments[1], texpires = arguments[2], tstr = "";
127                 if (isArray(tvalue)) {
128                     tstr = ArrayToString(tvalue);
129                 } else if (isObject(tvalue)) {
130                     tstr = ObjectToString(tvalue);
131                 } else {
132                     tstr = tvalue.toString();
133                 }
134                 tstr = tname + "=" + escape(tvalue) + ";expires=" + texpires.toGMTString() + ";";
135             } else if (arguments.length == 5) {
136                 var tname = arguments[0], tvalue = arguments[1], texpires = arguments[2], tdomain = arguments[3], tpath = arguments[4], tstr = "";
137                 if (isArray(tvalue)) {
138                     tstr = ArrayToString(tvalue);
139                 } else if (isObject(tvalue)) {
140                     tstr = ObjectToString(tvalue);
141                 } else {
142                     tstr = tvalue.toString();
143                 }
144                 tstr = tname + "=" + escape(tvalue) + ";expires=" + texpires.toGMTString() + ";domain=" + tdomain + ";path=" + tpath + ";";
145             }
146             alert(tstr);
147             this.Set(tstr);
148         },
149         Query: function() {
150             /*
151             *   Cookie.Query(); 返回所有Cookie值组成的Object;
152             *   Cookie.Query(string); 返回指定名称的Object; 失败则返回 undefined;
153             *   Cookie.Query(string, Object); 为指定对象写入指定名称的Object,并返回; 失败则返回 undefined;
154             */
155             var tname = tvalue = "", tright = -1;
156             var tstr = this.Get();
157             var tObj = {};
158             if (arguments.length == 0) {
159                 var i = 0;
160                 do {
161                     tname = trim(tstr.slice(i, tstr.indexOf("=", i)));
162                     tright = tstr.indexOf(";", i);
163                     if (tright == -1) {
164                         tvalue = unescape(tstr.slice(tstr.indexOf("=", i) + 1, tstr.length));
165                     } else {
166                         tvalue = unescape(tstr.slice(tstr.indexOf("=", i) + 1, tright));
167                     }
168                     tObj[tname] = tryEval(tvalue);
169                     i = tstr.indexOf(";", i) == -1 ? -1 : tstr.indexOf(";", i) + 1;
170                 } while (i != -1);
171 
172             } else {
173                 tname = arguments[0];
174                 if (tstr.indexOf(tname) == -1) return undefined;
175                 var i = tstr.indexOf(tname);
176                 tname = trim(tstr.slice(i, tstr.indexOf("=", i)));
177                 tright = tstr.indexOf(";", tstr.indexOf(tname)) == -1 ? tstr.length : tstr.indexOf(";", tstr.indexOf(tname));
178                 tvalue = unescape(tstr.slice(tstr.indexOf(tname) + tname.length + 1, tright));
179 
180                 if (arguments.length == 1) {
181                     tObj = tryEval(tvalue);
182                 } else if (arguments.length == 2) {
183                     tObj = arguments[1];
184                     tObj[tname] = tryEval(tvalue);
185                 }
186             }
187             return tObj;
188         },
189         Update: function() {
190             return this.Write.apply(this, arguments);
191         },
192         Delete: function() {
193             if (arguments.length == 1) {
194                 var varname = arguments[0];
195                 if (this.Query(varname)) {
196                     this.Update(varname, "", new Date(1970, 01, 01));
197                 }
198             }
199         }
200     }
201 

其中有一个从字符串eval 成对象的执行,以及从Object 或者 Array 对象获得对应字符串形式的功能函数,模拟了一些JSON的操作.当然,并不能存储所有的JavaScript 对象,仅仅满足一部分,我已经感觉够用了.

本文示例源代码或素材下载

上一页  1 2 3 4 

Tags:使用 JavaScript Cookie

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接