一個操作客戶端cooke的類

//***************************************
//            HttpCookie  
//
//   編寫: 靜靜的黎明
//   功能: 客戶端Cookie操作封裝,
//         支持多值cookie
//
//***************************************

//
// 填補 ie 5.0的 Array.splice 函數
//
if(new Array().splice == null)
{
/// usage: array.splice(startPos,deleteCount,[item1,item2,...]);for ie5.0
Array.prototype.splice = function()
{
if(arguments.length < 2 || arguments[1] < 0)
return new Array();

var endPoint1 = arguments[0];
if(endPoint1 < 0 && Math.abs(endPoint1) > this.length)
endPoint1 = 0;

var startPoint2 = (endPoint1 < 0)? (this.length + endPoint1 + arguments[1]) : (endPoint1 + arguments[1]);

var bArray = this.slice(0,endPoint1);
var dArray = this.slice(endPoint1,startPoint2);
var eArray = this.slice(startPoint2);
var nArray = new Array();
for(var i = 2, al = arguments.length; i < al; i++)
{
nArray.push(arguments[i]);
}
var fArray = bArray.concat(nArray,eArray);

for(var i = 0, al = fArray.length; i < al; i++)
{
this[i] = fArray[i];
}
this.length = fArray.length;

return dArray;
}
}

//
// NameValueCollection 類
//
function NameValueCollection()
{
this.__keys = new Array();
this.__values = new Object();

this.__checkArg = function()
{
for(var i = 0, j = arguments.length; i < j; i++)
{
if(typeof arguments[i] != "string")
return false;
}
return true;
}

//
// 返回包含當前實例的所有鍵的字符數組
//
this.allKeys = function()
{
return this.__keys.concat();
}


//
// 獲取與 NameValueCollection 中的指定鍵關聯的值(字符數組)。
//
this.getValues = function(name)
{
if(!this.__checkArg(name))
{
throw new Error("Invalid type on NameValueCollection.getValues's argument.");
}

var values = this.__values[name.toLowerCase()];

return (values instanceof Array)? values : null;
}

//
// 將具有指定名稱和值的項添加到 NameValueCollection。
//
this.add = function(name, value)
{
if(!this.__checkArg(name, value))
{
throw new Error("Invalid type on NameValueCollection.add's argument");
}

var key = name.toLowerCase();

if(this.__values[key] == null)
{
this.__keys[this.__keys.length] = name;
this.__values[key] = new Array(value);
}
else
{
this.__values[key][this.__values[key].length] = value;
}
}

//
// 獲取與 NameValueCollection 中的指定鍵關聯的值,
// 如果對應多個值,以逗號分割,參考getValues。
//
this.get = function(name)
{
if(!this.__checkArg(name))
{
throw new Error("Invalid type on NameValueCollection.get's argument");
}

var value = this.__values[name.toLowerCase()];

return (value instanceof Array)? value.join(",") : null;
}

//
// 設置或改變(該項不爲空)NameValueCollection 中某個項的值。
//
this.set = function(name, value)
{
if(!this.__checkArg(name, value))
{
throw new Error("Invalid type on NameValueCollection.set's argument");
}

var key = name.toLowerCase();

if(this.__values[key] == null)
{
this.__keys[this.__keys.length] = name;
}

this.__values[key] = new Array(value);
}

this.hasKeys = function()
{
return this.__keys.length > 0;
}
//
// 清空NameValueCollection的所有鍵值。
//
this.clear = function()
{
this.__keys = new Array();
this.__values = new Object();
}

//
// 將具有指定鍵的項從當前實例中刪除。
//
this.remove = function(name)
{
if(!this.__checkArg(name))
{
throw new Error("Invalid type on NameValueCollection.remove's argument");
}

var key = name.toLowerCase();

if(this.__values[key] == null)
return;

for(var i = 0, j = this.__keys.length; i < j; i++)
{
if(this.__keys[i] == key)
{
this.__keys.splice(i, 1);
this.__values[key] = null;
return;
}
}
}
}
//
// 類HttpCookie
//
function HttpCookie(name)
{
//
// 私有屬性
//
this.__name;
this.__isExisted;
this.__expires;
this.__value;
this.__path;

//
// 公有屬性:values; 類型:JScript.Collections.NameValueCollection;
//
this.values;

//
// 判定當前的 HttpCookie 實例是否存在。
//
this.isExisted = function()
{
return this.__isExisted;
}

//
//  判斷當前的cookie是否具有子鍵。
//
this.hasKeys = function()
{
return this.values.hasKeys();
}

//
// 私有方法,分析cookie串
//
this.__analysisCooString = function(cstring)
{
var subCookies = cstring.split("&");

for(var k = 0, l = subCookies.length; k < l; k++)
{
var key, values, splitPos;

splitPos = subCookies[k].indexOf("=");

if(splitPos != -1)
{
key   = subCookies[k].substring(0, splitPos);
values = subCookies[k].substring(splitPos + 1);
values = values.split(",");

//插入鍵值
for(var m = 0, n = values.length; m < n; m++)

this.values.add(key, values[m]);
}
else
{
this.__value = subCookies[k];
}
}
}

//
// 私有方法,初始化 cookie 。
// 如果以參數 name 爲名字的cookie存在,則導入該cookie;
// 調用isExisted 方法則返回真,否則初始化一個新實例;調用
// isExisted()返回 false。
//
this.__init = function(name)
{
if(typeof name != "string")
throw new Error("Invalid type on HttpCookie's argument");

this.__name = (name != null)? name : "";
this.__value = "";
this.__isExisted = false;
this.__expires = null;
this.__path = "/";
this.values = new NameValueCollection();

var cookies = document.cookie.split("; ");

for(var i = 0, j = cookies.length; i < j; i++)
{
var name, value, splitPos;

splitPos = cookies[i].indexOf("=");

if(splitPos != -1)
{
name  = cookies[i].substring(0, splitPos);
value = cookies[i].substring(splitPos + 1);
}
else
{
name = cookies[i];
}

if(this.__name.toLowerCase() == name.toLowerCase())
{
this.__isExisted = true;

if(value == null) return;

this.__analysisCooString(value);

break;
}
}
}

//
// 返回當前HttpCookie實例的名字。
//
this.getName = function()
{
return this.__name;
}

//
// 設定當前 HttpCookie 實例的值
//
this.setValue = function(value)
{
if(value != null)
{
this.__value = value.toString();
this.values = new NameValueCollection();
}
}

//
// 返回 當前 HttpCookie 實例的值
//
this.getValue = function()
{
var value = new String();

if(this.values.hasKeys())
{
var keys = this.values.allKeys();

for(var i = 0, j = keys.length; i < j; i++)
{
value += "&" + keys[i] + "=" + this.values.get(keys[i]);
}
}

return  this.__value + ((this.__value == "")? value.substring(1) : value);

}

//
// 設定當前 HttpCookie 實例的過期時間;以秒爲單位
// 當該方法未被調用時,實例默認跟隨窗體存活期。
//
this.setExpires = function(secs)
{
if(typeof secs == "number")
{
var sysExpiresTime = new Date().getTime() + parseInt(secs) * 1000;

this.__expires = new Date(sysExpiresTime).toGMTString();
}
}

//
// 設定當前 HttpCookie 實例的的有效跟目錄。
//
this.setPath = function(path)
{
if(path != null)
this.__path = path.toString();
}

//
// 將當前的HttpCookie實例配置寫入 cookie。
//
this.save = function()
{
document.cookie = this.__name + "=" + this.getValue() + ";" +
((this.__expires != null)? "expires=" +this.__expires + ";" : "") +
"path=" + this.__path + ";";

}
//初始化
this.__init(name);
}

測試代碼:
<script>

function enumCookieValues(coo)
{
//枚舉
var keys = coo.values.allKeys();

alert("用HttpCookie.values.get枚舉");

for(var i = 0, j = keys.length; i < j; i++)
{
alert( "key=" + keys[i] + "; value=" + coo.values.get(keys[i]) );
}
////////////////////////////////////////////////////
alert("用HttpCookie.values.getValues枚舉");

for(var i = 0, j = keys.length; i < j; i++)
{
var valueArr = coo.values.getValues(keys[i]);

for(var k = 0, l = valueArr.length; k < l; k++)
{
alert("key=" + keys[i] + ";value=" + valueArr[k]);
}
}

}
//簡單的單值cookie的例子,需要效果請拿掉註釋
 
// var coo = new HttpCookie("cookie");
// coo.setValue("cookie的值");
// coo.save();
// alert(coo.getValue());

// 多值cookie
var coo = new HttpCookie("cookie1");

if(coo.isExisted() == false)
{
         //set 重寫或設定, add方法不重寫原來的鍵值
coo.values.set("language", "javascript");

coo.values.add("method", "post");

coo.values.add("method", "get");

coo.save();

//枚舉
enumCookieValues(coo);

}
else
{
//枚舉
enumCookieValues(coo);

//刪除一個鍵值
coo.values.remove("method");
//設定過期時間秒數,這裏設定爲當前時間,就是刪除
coo.setExpires(0);

coo.save();

}
</script>

發佈了42 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章