您好,欢迎访问一九零五行业门户网

javascript中方便增删改cookie的一个类_javascript技巧

主要是通过对document.cookie字符串的分析来进行功能的组装的。
温习一下javascript中对cookie的操作:
增加cookie可以用document.cookie=userid=111;来实现
完整版可以用:document.cookie=userid=111;domain=.google.com;path=\;secure=secure;expire=+date.togmtstring();
可以设置cookie的过期时间,域名,路径
需要删除只要将expire的时间设为现在之前就可以了
现在上我修改的javascript.cookie.js的类
复制代码 代码如下:
/*
cookie helper class
easy to write,get,delete
*/
var mycookie={
get:function(name){
if(typeof name != undefined)
{
//if name given call the get value function
return mycookie_get(name);
}else{
//if name is not given,i want get all the cookie item
return mycookie_getall();
}
},
add:function(name,value,options){
//write the cookie
mycookie_add(name,value,options);
},
delete:function(name){
//delete the cookie
mycookie_add(name,null);
}
}
string.prototype.trim = function()
{
return this.replace(/^\s+/g,).replace(/\s+$/g,);
}
/*
cookie write function
@name:the cookie name not null
@value:the cookie value null==delete the cookie
@option:{expires:expire time;path:/;domain:localhost;secure:secure}
*/
function mycookie_add(name,value,options)
{
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toutcstring)) {
var date;
if (typeof options.expires == 'number') {
date = new date();
date.settime(date.gettime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toutcstring(); // use expires attribute, max-age is not supported by ie
}
var path = options.path ? '; path=' + options.path : '';
var domain = options.domain ? '; domain=' + options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeuricomponent(value), expires, path, domain, secure].join('');
}
}
/*
get the name cookie
@name:the cookie's name
*/
function mycookie_get(name)
{
var cookievalue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i var cookie = cookies[i].trim();
// does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookievalue = decodeuricomponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookievalue;
}
/*
get all the cookie return as a json
*/
function mycookie_getall()
{
var cookiearray = new array();
var str=;
var temp;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i var cookie = cookies[i].trim();
temp=cookie.split('=');
//take the
cookiearray.push({\name\:\+decodeuricomponent(temp[0])+\,\value\:\+decodeuricomponent(temp[1])+\});
}
str=cookiearray.join(,);
}
str=[+str+];
return eval('('+str+')');
}
调用也是相当简单
复制代码 代码如下:
mycookie.add(useraccount,admin,{expires:5});//加入一个期限为5天的cookie
alert(mycookie.get(useraccount));//取出cookie
cookies=mycookie.get();//得到所有的cookie
for(var i=0;i{
alert(cookies[i][name]+:+cookies[i][value]);
}
mycookie.delete(useraccount);//删除刚刚添加的cookie
alert(mycookie.get(useraccount));
其它类似信息

推荐信息