JS读写客户端Cookies

把前一陈子写的一段JS发出来,代码很简单,但希望对一些人有帮助。少写几行重复的代码

CookiesHelper.js

//Author:fengxu

//Date:2009-08-18

//Summary:读写Cookies

//

CookiesHelper = function() {

var d = new Date()

d.setDate(d.getDate() + 15);

this.expriceDate = d.toUTCString();             //设置过期时间

this.unDesCodeValue = document.cookie;          //未解码的Cookies值

this.cookies = new Array();  //cookies对象

if (this.unDesCodeValue != '' && this.unDesCodeValue != null) {

var tempArr = this.unDesCodeValue.split(';'); //将cookies对象分开

for (var i = 0; i < tempArr.length; i++) {

var arrCookItem;

if (tempArr[i].split('=').length > 2)        //如果cookie项有子cookies值,暂时不支持asp.net的写法Response.Cookies["Name"]["Key"]

{

//                arrCookItem = new Array();

//                arrCookItem.push(tempArr[i].substring(0, tempArr[i].indexOf('=') - 1));

//                arrCookItem.push(tempArr[i].substring(tempArr[i].indexOf('='), tempArr[i.length-1]).split('&'))

//                arrCookItem[0] = decodeURIComponent(arrCookItem[0]);  //解码名称和值

}

else {

arrCookItem = tempArr[i].split('=');   //将cookies名称和值分开

arrCookItem[0] = decodeURIComponent(arrCookItem[0]);  //解码名称和值

arrCookItem[1] = decodeURIComponent(arrCookItem[1]);

this.cookies.push(arrCookItem);        //得到一个二维数组,对应cookies值

}

}

}

}

//取cookies值

CookiesHelper.prototype.GetCookieValue = function(cookName) {

if (null == this.cookies) return '';

var name;

var value;

var arrTemp;

for (var i = 0; i < this.cookies.length; i++) {

arrTemp = this.cookies[i];

name = arrTemp[0];

value = arrTemp[1];

if (name == cookName)

return value;

}

return null;

}

CookiesHelper.prototype.SetCookiesValue = function(cookName, cookValue) {

var name;

var value;

var arrTemp;

var isAdd = true;  //判断是否是添加新Cookies值

for (var i = 0; i < this.cookies.length; i++) {

arrTemp = this.cookies[i];

name = arrTemp[0];

value = arrTemp[1];

if (name == cookName) {

this.cookies[i][1] = cookValue;     //如果有则修改;

document.cookie = cookName + '=' + encodeURIComponent(cookValue) + "; expires=" + this.expriceDate + "";

isAdd = false;

break;

}

}

if (isAdd) {

var newCookie = new Array();            //如果没有则添加;

newCookie.push(cookName);

newCookie.push(cookValue);

this.cookies.push(newCookie);

document.cookie = cookName + '=' + encodeURIComponent(cookValue) + "; expires=" + this.expriceDate + "";

}

}

CookiesHelper.prototype.AddCookie = function(cookName, cookValue) {

this.SetCookiesValue(cookName, cookValue);

}

//删除cookie

CookiesHelper.prototype.DeleteCookie = function(cookieName) {

if (this.cookies.length == 0)

return;

else {

var d = new Date();

d.setDate(d.getDate() - 1);

var realCookies = '';

var documentCookie = new Array();       //删除时要更新数组

for (var i = 0; i < this.cookies.length; i++) {

arrTemp = this.cookies[i];

name = arrTemp[0];

value = arrTemp[1];

if (name.replace(/\s/g,'') == (cookieName)) {

realCookies += name + '=' + encodeURIComponent(value) + "; expires=" + d.toGMTString() + "";

break;

}

else {

var tempArr = new Array();

tempArr.push(name);

tempArr.push(value);

documentCookie.push(tempArr);

//                realCookies += encodeURIComponent(name) + '=';

//                realCookies += encodeURIComponent(value);

//                realCookies += "; expires=" + this.expriceDate + ";";

}

}

this.cookies = documentCookie;

alert(realCookies);

document.cookie = realCookies;

}

}

var cookieHelper = new CookiesHelper();

CookiesHelper.prototype.DeCodeCookieValue = function() {

//    this.desCodeValue = decodeURIComponent(this.cookies)

}

//CookiesHelper.registerClass('CookiesHelper');

//if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
使用方法:
    <script language="javascript" type="text/javascript">

//写CookieS

cookieHelper.SetCookiesValue(CookieName,CookieValue);

//读Cookies

var cookieValue = cookieHelper.GetCookieValue(CookieName);

//删除

cookieHelper.DeleteCookie(CookieName);

</script>

你可能感兴趣的:(Cookies)