javascript中关于cookie的常见操作汇总整理
同样的Cookie的内容的字符限制针对不同的Cookie版本也有不同。在Cookie Version 0中,某些特殊的字符,例如:空格,方括号,圆括号,等于号(=),逗号,双引号,斜杠,问号,@符号,冒号,分号都不能作为Cookie的内容。这也就 是为什么我们在例子中设定Cookie的内容为"Test_Content"的原因
<SCRIPT LANGUAGE="JavaScript">
function GetCookieVal(offset)
//获得Cookie解码后的值
{
var endstr = documents.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = documents.cookie.length;
return unescape(documents.cookie.substring(offset, endstr));
}如果点了确定,只要不清cookie,以后访问都不会再提示,如果不点确定则每次都会提示。放在js文件里,全站包含
<!--
var the_cookie = document.cookie;
var broken_cookie = the_cookie.split(":");
var the_visiteraccepted = unescape(broken_cookie[1]);
//
if (the_visiteraccepted=="undefined"){
var tmp=confirm('中国人何时何地。');
if(tmp==false){
window.close();
}else{
var the_visiteraccepted = 1;
var the_cookie = "ILoveChina=visiteraccepted:" + escape(the_visiteraccepted);
document.cookie = the_cookie;
}
}
//-->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">//写cookies函数
function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
{
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//读取cookies函数
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;}
function delCookie(name)//删除cookie
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
//简单例子
//SetCookie ("name", www.3ppt.com)
//alert(getCookie(name));
</script>
最后提供一个操作类
//Cookie类
function Cookie(){
Cookie.prototype=new Object();//继承自Object
this.constructor=Cookie;
//重写toString函数,生成为document.cookie形式的字符串
Cookie.prototype.toString=function(){
var data=[];//存放cookie子键
var _cookie=[];//存放cookie键值对
for(var prop in this){
switch(true){
case prop=="name":
_cookie[0]=prop;
continue;
case prop=="expires":
_cookie[1]=prop+"="+this[prop];
continue;
case prop=="path":
_cookie[2]=prop+"="+this[prop];
continue;
case prop=="secure":
_cookie[3]=prop+"="+this[prop];
continue;
case prop=="domain":
_cookie[4]=prop+"="+this[prop];
continue;
}
data[data.length]=prop+":"+escape(this[prop]);
}
_cookie[0]+="="+data.join("&");
return _cookie.join(";");
};
return this;//返回cookie对象
}
//将字符串格式化为cookie, 静态函数
Cookie.format=function(str){
var cookie=new Cookie();
var start=str.indexOf("=");
cookie.name=str.substring(0,start);
var value=str.substring(start+1);
value=value.split('&');
for ( var v in value ){
v=v.split(':');
this[v[0]]=unescape(v[1]);
}
return cookie;
};
//获取当前所有cookie, 静态对象
Cookie.cookies=(function(){
var cookie=document.cookie;
var obj;
cookie=cookie.split(';');
for (var c in cookie){
c=Cookie.format(c);
obj[c.name]=c;
}
return obj;
})();
//添加cookie, 静态函数
Cookie.Add=function(cookie){
if(cookie instanceof Cookie){
document.cookie=cookie.toString();
}
//else console.log("参数错误");
}
//删除cookie, 静态函数
Cookie.Remove=function(cookie){
if(cookie instanceof Cookie){
var time=new Data();
time.setFullYear(time.getFullYear()-10);
cookie.expires=time.toGMTString();
document.cookie=cookie.toString();
}
}