一个方便在客户端操作cookies的javascript类(需要prototype),部分代码参考了《javascript 权威指南》
document.xCookie
=
Class.create();
/*
操作cookies的对象
*/
Object.extend(document.xCookie,{
/*
//字段说明/
$document:私有字段,当前需要操作cookies的文档对象
//方法说明
void save(void):公有方法,保存所有cookies
void clear(void):公有方法,清除所有cookies
<document.xCookie.cookie> $(name):返回指定name的cookie
*/
/* 加载所有cookies,再需要操作cookies时,必须先执行该方法 */
load: function (d) {
this .$document = d || document;
var cookies = this .$document.cookie;
var cookieRegEx =/ w += [ ^ ;] +/ g;
var matchs = cookies.match(cookieRegEx);
for ( var i = 0 ; matchs && i < matchs.length; i ++ )
{
var arr = matchs[i].split( ' = ' );
this [arr[ 0 ]] = new document.xCookie.cookie( this .$document,arr[ 0 ],arr[ 1 ]);
}
},
save: function () {
for ( var prop in this )
{
if ((prop.charAt( 0 ) != ' $ ' ) && (( typeof this [prop]) != ' function ' ) && this [prop].write)
this [prop].write();
}
},
clear: function () {
for ( var prop in this )
if ( this [prop].remove) this [prop].remove();
},
$: function (name) {
if ( this [name])
return this [name];
else
{
this [name] = new document.xCookie.cookie( this .$document,name);
return this [name];
}
}
});
document.xCookie.cookie = Class.create();
/*
操作指定name值的cookie的类,非必要时,可以不直接使用该类。
*/
document.xCookie.cookie.prototype = {
/*
//字段说明/
$document:私有字段,当前需要操作cookie的文档对象
$name:私有字段,当前cookie的名字
$val:私有字段,当前cookie的属性/值对
$timeout:私有字段,cookie过时设置,单位为分钟
$path:私有字段,cookie有效路径
$domain:私有字段,cookie有效域
$secure:私有字段,cookie传输加密方式
//方法说明
void initialize(document,name,val,timeout,path,domain,secure):构造函数
void read(void):公有方法,读取当前cookie的属性/值对
void write(void):公有方法,写入当前cookie的属性/值对
void remove(void):公有方法,删除当前cookie
String toString(void):公有方法,返回当前cookie的属性/值对的字符串表示
*/
initialize: function (document,name,val,timeout,path,domain,secure) {
this .$document = document;
this .$name = name;
if (val)
this .$val = val;
else
this .$val = '' ;
if (timeout)
this .$expiration = new Date(( new Date()).getTime() + timeout * 60000 );
else
this .$expiration = null ;
if (path)
this .$path = path;
else
this .$path = null ;
if (domain)
this .$domain = domain;
else
this .$domain = null ;
if (secure)
this .$secure = true ;
else
this .$secure = false ;
this .read();
},
read: function () {
var valRegEx =/ w + :[ ^& ] +/ g;
var matchs = this .$val.match(valRegEx);
for ( var i = 0 ;matchs && i < matchs.length; i ++ )
{
var arr = matchs[i].split( ' : ' );
this [arr[ 0 ]] = unescape(arr[ 1 ]);
}
},
write: function () {
var val = this .toString();
var cookie = this .$name + ' = ' + val;
if ( this .$expiration) cookie += " ;expires= " + this .$expiration.toGMTString();
if ( this .$path) cookie += " ;path= " + this .$path;
if ( this .$domain) cookie += " ;domain= " + this .$domain;
if ( this .$secure) cookie += " ;secure " ;
this .$document.cookie = cookie;
},
remove: function () {
var val = '' ;
for ( var prop in this )
{
if ((prop.charAt( 0 ) == ' $ ' ) || (( typeof this [prop]) == ' function ' ))
continue ;
this [prop] = null ;
}
this .$expiration = new Date( 1970 , 1 , 2 , 0 , 0 , 0 );
},
toString: function () {
var val = '' ;
for ( var prop in this )
{
if ((prop.charAt( 0 ) == ' $ ' ) || (( typeof this [prop]) == ' function ' ))
continue ;
if (val != '' ) val += ' % ' ;
val = prop + ' : ' + escape( this [prop]);
}
return val;
}
}
/* xCookie对象等同于document.xCookie对象 */
var xCookie = Class.create();
Object.extend(xCookie,document.xCookie);
/*
操作cookies的对象
*/
Object.extend(document.xCookie,{
/*
//字段说明/
$document:私有字段,当前需要操作cookies的文档对象
//方法说明
void save(void):公有方法,保存所有cookies
void clear(void):公有方法,清除所有cookies
<document.xCookie.cookie> $(name):返回指定name的cookie
*/
/* 加载所有cookies,再需要操作cookies时,必须先执行该方法 */
load: function (d) {
this .$document = d || document;
var cookies = this .$document.cookie;
var cookieRegEx =/ w += [ ^ ;] +/ g;
var matchs = cookies.match(cookieRegEx);
for ( var i = 0 ; matchs && i < matchs.length; i ++ )
{
var arr = matchs[i].split( ' = ' );
this [arr[ 0 ]] = new document.xCookie.cookie( this .$document,arr[ 0 ],arr[ 1 ]);
}
},
save: function () {
for ( var prop in this )
{
if ((prop.charAt( 0 ) != ' $ ' ) && (( typeof this [prop]) != ' function ' ) && this [prop].write)
this [prop].write();
}
},
clear: function () {
for ( var prop in this )
if ( this [prop].remove) this [prop].remove();
},
$: function (name) {
if ( this [name])
return this [name];
else
{
this [name] = new document.xCookie.cookie( this .$document,name);
return this [name];
}
}
});
document.xCookie.cookie = Class.create();
/*
操作指定name值的cookie的类,非必要时,可以不直接使用该类。
*/
document.xCookie.cookie.prototype = {
/*
//字段说明/
$document:私有字段,当前需要操作cookie的文档对象
$name:私有字段,当前cookie的名字
$val:私有字段,当前cookie的属性/值对
$timeout:私有字段,cookie过时设置,单位为分钟
$path:私有字段,cookie有效路径
$domain:私有字段,cookie有效域
$secure:私有字段,cookie传输加密方式
//方法说明
void initialize(document,name,val,timeout,path,domain,secure):构造函数
void read(void):公有方法,读取当前cookie的属性/值对
void write(void):公有方法,写入当前cookie的属性/值对
void remove(void):公有方法,删除当前cookie
String toString(void):公有方法,返回当前cookie的属性/值对的字符串表示
*/
initialize: function (document,name,val,timeout,path,domain,secure) {
this .$document = document;
this .$name = name;
if (val)
this .$val = val;
else
this .$val = '' ;
if (timeout)
this .$expiration = new Date(( new Date()).getTime() + timeout * 60000 );
else
this .$expiration = null ;
if (path)
this .$path = path;
else
this .$path = null ;
if (domain)
this .$domain = domain;
else
this .$domain = null ;
if (secure)
this .$secure = true ;
else
this .$secure = false ;
this .read();
},
read: function () {
var valRegEx =/ w + :[ ^& ] +/ g;
var matchs = this .$val.match(valRegEx);
for ( var i = 0 ;matchs && i < matchs.length; i ++ )
{
var arr = matchs[i].split( ' : ' );
this [arr[ 0 ]] = unescape(arr[ 1 ]);
}
},
write: function () {
var val = this .toString();
var cookie = this .$name + ' = ' + val;
if ( this .$expiration) cookie += " ;expires= " + this .$expiration.toGMTString();
if ( this .$path) cookie += " ;path= " + this .$path;
if ( this .$domain) cookie += " ;domain= " + this .$domain;
if ( this .$secure) cookie += " ;secure " ;
this .$document.cookie = cookie;
},
remove: function () {
var val = '' ;
for ( var prop in this )
{
if ((prop.charAt( 0 ) == ' $ ' ) || (( typeof this [prop]) == ' function ' ))
continue ;
this [prop] = null ;
}
this .$expiration = new Date( 1970 , 1 , 2 , 0 , 0 , 0 );
},
toString: function () {
var val = '' ;
for ( var prop in this )
{
if ((prop.charAt( 0 ) == ' $ ' ) || (( typeof this [prop]) == ' function ' ))
continue ;
if (val != '' ) val += ' % ' ;
val = prop + ' : ' + escape( this [prop]);
}
return val;
}
}
/* xCookie对象等同于document.xCookie对象 */
var xCookie = Class.create();
Object.extend(xCookie,document.xCookie);
举例:
<
script type
=
"
text/javascript
"
src
=
"
lib/prototype-1.4.0.js
"
></
script
>
<!--
需要prototype框架支持
-->
< script type = " text/javascript " >
window.onload = function () {
document.xCookie.load();
document.xCookie.$( ' myname ' )[ ' firstName ' ] = ' Terry ' ;
document.xCookie.$( ' myname ' )[ ' lastName ' ] = ' King ' ;
document.xCookie.save();
alert(document.xCookie.$( ' myname ' ).toString());
}
</ script >
< script type = " text/javascript " >
window.onload = function () {
document.xCookie.load();
document.xCookie.$( ' myname ' )[ ' firstName ' ] = ' Terry ' ;
document.xCookie.$( ' myname ' )[ ' lastName ' ] = ' King ' ;
document.xCookie.save();
alert(document.xCookie.$( ' myname ' ).toString());
}
</ script >