读取cookie
function getCookie(c_name){
if (document.cookie.length>0){ //先查询cookie是否为空,为空就return ""
c_start=document.cookie.indexOf(c_name + "=") //通过String对象的indexOf()来检查这个cookie是否存在,不存在就为 -1
if (c_start!=-1){
c_start=c_start + c_name.length+1 //最后这个+1其实就是表示"="号啦,这样就获取到了cookie值的开始位置
c_end=document.cookie.indexOf(";",c_start) //其实我刚看见indexOf()第二个参数的时候猛然有点晕,后来想起来表示指定的开始索引的位置...这句是为了得到值的结束位置。因为需要考虑是否是最后一项,所以通过";"号是否存在来判断
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end)) //通过substring()得到了值。想了解unescape()得先知道escape()是做什么的,都是很重要的基础,想了解的可以搜索下,在文章结尾处也会进行讲解cookie编码细节
}
}
return ""
}
设置
function setCookie(c_name, value, expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie=c_name+ "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
使用方法:setCookie('username','Darren',30)
/*
*Name: Cookie操作
*Date: 2011年5月20日16:57
*/
var Util = {};
Util.Cookie = {
get:function(a){
var b = "";
var c = a + "=";
var d = document.cookie;
if (d.length > 0) {
g = d.indexOf(c);
if (g != -1) {
g += c.length;
f = d.indexOf(";", g);
if (f == -1)
f = d.length;
b = unescape(d.substring(g, f));
};
};
return b;
},
set:function(a, b){
var c = "";
var h = Util.Config.cookieHours;
if (h != null) {
c = new Date((new Date()).getTime() + h * 3600000);
c = "; expires=" + c.toGMTString();
};
document.cookie = a + "=" + escape(b) + c +";path=/";
},
del:function(a){
document.cookie = a + "=;path=/;" + "expires=" + (new Date(0)).toGMTString();
}
};
//写入cookie
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
//读取cookie
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}
先百度一下啊,你这个问题一大堆啊