Cookie = {};
/*
* Date对象的setTime方法是设置距离1970-01-01以来的毫秒数,设置到对象里去,返回的是据那以后的毫秒数而不是原对象.
* 如果Cookie 不设置 expires 属性,或者expires 时间比本地时间少,那么将会在下一次浏览时过期.
* document.cookie 对象返回的是所有值的字符串形式,不包含 expires 或者其他.
*
*/
(function() {
var nDay = 24 * 60 * 60 * 1000;
var isString = function(v) {
return typeof v === "string";
}
var isArray = function(v) {
return Object.prototype.toString.apply(v) == "[object Array]";
}
var isObject = function(v) {
return v && typeof v == "object";
}
var isDate = function(v) {
return Object.prototype.toString.apply(v) == "[object Date]";
}
var getTime = function() {
return new Date().getTime();
}
var trim = function(val) {
return (val || "").replace(/^\s+|\s+$/g, "");
}
var tryEval = function(val) {
var Obj, e;
try {
Obj = eval(val);
} catch (e) {
Obj = val;
}
return Obj;
}
var ObjectToString = function(o) {
var tstr = "{";
for (var v in o) {
if (isArray(o[v])) {
tstr += v + ":" + ArrayToString(o[v]) + ",";
} else if (isObject(o[v])) {
tstr += v + ":" + ObjectToString(o[v]) + ",";
} else if (isString(o[v])) {
tstr += v + ":\"" + o[v].toString() + "\",";
} else {
tstr += v + ":" + o[v].toString() + ",";
}
}
return tstr.slice(0, -1) + "}";
}
var ArrayToString = function(o) {
var tstr = "[";
for (var v in o) {
if (isArray(o[v])) {
tstr += ArrayToString(o[v]) + ",";
} else if (isObject(o[v])) {
tstr += ObjectToString(o[v]) + ",";
} else {
tstr += o[v].toString() + ",";
}
}
return tstr.slice(0, -1) + "]"; ;
}
Cookie = {
Get: function() {
return document.cookie;
},
Set: function(val) {
document.cookie = val;
},
Clear: function() {
var temp = this.Query();
var o;
for (o in temp) {
this.Delete(o);
}
},
GetDayTime: function(val) {
var texpires = new Date();
texpires.setTime(texpires.getTime() + val * nDay);
return texpires;
},
Write: function() {
/*
* Cookie.Write(Object); 写入对象,名称为main;
* Cookie.Write(varname, Object); varname:变量名, Object:写入对象;
* Cookie.Write(Object, Date); Object:写入对象, Date:过期时间;
* Cookie.Write(varname, Object, Date); varname:变量名, Object:写入对象, Date:过期时间;
* Cookie.Write(varname, Object, Date, Domain, Path); varname:变量名, Object:写入对象, Date:过期时间, Domain:域, Path: 子目录;
*/
if (arguments.length == 1) {
var tvalue = arguments[0];
var tstr = "";
var texpires = new Date(); texpires.setTime(texpires.getTime() + 1 * nDay);
if (isArray(tvalue)) {
tstr = ArrayToString(tvalue);
} else if (isObject(tvalue)) {
tstr = ObjectToString(tvalue);
} else {
tstr = tvalue.toString();
}
tstr = "main=" + escape(tstr) + ";expires=" + texpires.toGMTString() + ";";
} else if (arguments.length == 2) {
var tname, tvalue, texpires, tstr = "";
if (isDate(arguments[1])) {
tname = "main";
tvalue = arguments[0];
texpires = arguments[1];
} else {
tname = arguments[0];
tvalue = arguments[1];
texpires = new Date(); texpires.setTime(texpires.getTime() + 1 * nDay);
}
if (isArray(tvalue)) {
tstr += ArrayToString(tvalue);
} else if (isObject(tvalue)) {
tstr += ObjectToString(tvalue);
} else {
tstr = tvalue.toString();
}
tstr = tname + "=" + escape(tvalue) + ";expires=" + texpires.toGMTString() + ";";
} else if (arguments.length == 3) {
var tname = arguments[0], tvalue = arguments[1], texpires = arguments[2], tstr = "";
if (isArray(tvalue)) {
tstr = ArrayToString(tvalue);
} else if (isObject(tvalue)) {
tstr = ObjectToString(tvalue);
} else {
tstr = tvalue.toString();
}
tstr = tname + "=" + escape(tvalue) + ";expires=" + texpires.toGMTString() + ";";
} else if (arguments.length == 5) {
var tname = arguments[0], tvalue = arguments[1], texpires = arguments[2], tdomain = arguments[3], tpath = arguments[4], tstr = "";
if (isArray(tvalue)) {
tstr = ArrayToString(tvalue);
} else if (isObject(tvalue)) {
tstr = ObjectToString(tvalue);
} else {
tstr = tvalue.toString();
}
tstr = tname + "=" + escape(tvalue) + ";expires=" + texpires.toGMTString() + ";domain=" + tdomain + ";path=" + tpath + ";";
}
alert(tstr);
this.Set(tstr);
},
Query: function() {
/*
* Cookie.Query(); 返回所有Cookie值组成的Object;
* Cookie.Query(string); 返回指定名称的Object; 失败则返回 undefined;
* Cookie.Query(string, Object); 为指定对象写入指定名称的Object,并返回; 失败则返回 undefined;
*/
var tname = tvalue = "", tright = -1;
var tstr = this.Get();
var tObj = {};
if (arguments.length == 0) {
var i = 0;
do {
tname = trim(tstr.slice(i, tstr.indexOf("=", i)));
tright = tstr.indexOf(";", i);
if (tright == -1) {
tvalue = unescape(tstr.slice(tstr.indexOf("=", i) + 1, tstr.length));
} else {
tvalue = unescape(tstr.slice(tstr.indexOf("=", i) + 1, tright));
}
tObj[tname] = tryEval(tvalue);
i = tstr.indexOf(";", i) == -1 ? -1 : tstr.indexOf(";", i) + 1;
} while (i != -1);
} else {
tname = arguments[0];
if (tstr.indexOf(tname) == -1) return undefined;
var i = tstr.indexOf(tname);
tname = trim(tstr.slice(i, tstr.indexOf("=", i)));
tright = tstr.indexOf(";", tstr.indexOf(tname)) == -1 ? tstr.length : tstr.indexOf(";", tstr.indexOf(tname));
tvalue = unescape(tstr.slice(tstr.indexOf(tname) + tname.length + 1, tright));
if (arguments.length == 1) {
tObj = tryEval(tvalue);
} else if (arguments.length == 2) {
tObj = arguments[1];
tObj[tname] = tryEval(tvalue);
}
}
return tObj;
},
Update: function() {
return this.Write.apply(this, arguments);
},
Delete: function() {
if (arguments.length == 1) {
var varname = arguments[0];
if (this.Query(varname)) {
this.Update(varname, "", new Date(1970, 01, 01));
}
}
}
}
其中有一个从字符串eval 成对象的执行,以及从Object 或者 Array 对象获得对应字符串形式的功能函数,模拟了一些JSON的操作.当然,并不能存储所有的JavaScript 对象,仅仅满足一部分,我已经感觉够用了.
个人理解有限,请各位多多指教.
Javascript的匿名函数 :
Javascript的闭包 :
Cookie 文件的格式 :
您可能感兴趣的文章: