主要是通过对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 < cookies.length; 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 < cookies.length; 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<cookies.length;i++)
{
alert(cookies[i]["name"]+":"+cookies[i]["value"]);
}
myCookie.delete("useraccount");//删除刚刚添加的cookie
alert(myCookie.get("useraccount"));

标签:
增删改,cookie

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“javascript中方便增删改cookie的一个类”

暂无“javascript中方便增删改cookie的一个类”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。