javascript cookie操作类的实现代码小结附使用方法
更新时间:2010年06月02日 14:32:02 作者:
javascript cookie操作类的实现代码小结附使用方法,对于cookies操作不是很熟悉的朋友可以参考下。
第一种方法:cookie操作类,代码封装了,下面也有使用方法,大家可以参考下。
String.prototype.Trim = function()
{
return this.replace(/^\s+/g,"").replace(/\s+$/g,"");
}
function JSCookie()
{
this.GetCookie = function(key)
{
var cookie = document.cookie;
var cookieArray = cookie.split(';');
var getvalue = "";
for(var i = 0;i<cookieArray.length;i++)
{
if(cookieArray[i].Trim().substr(0,key.length) == key)
{
getvalue = cookieArray[i].Trim().substr(key.length + 1);
break;
}
}
return getvalue;
};
this.GetChild = function(cookiekey,childkey)
{
var child = this.GetCookie(cookiekey);
var childs = child.split('&');
var getvalue = "";
for(var i = 0;i < childs.length;i++)
{
if(childs[i].Trim().substr(0,childkey.length) == childkey)
{
getvalue = childs[i].Trim().substr(childkey.length + 1);
break;
}
}
return getvalue;
};
this.SetCookie = function(key,value,expire,domain,path)
{
var cookie = "";
if(key != null && value != null)
cookie += key + "=" + value + ";";
if(expire != null)
cookie += "expires=" + expire.toGMTString() + ";";
if(domain != null)
cookie += "domain=" + domain + ";";
if(path != null)
cookie += "path=" + path + ";";
document.cookie = cookie;
};
this.Expire = function(key)
{
expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() - 1);
var cookie = " " + key + "=e;expires=" + expire_time + ";"
document.cookie = cookie;
}
}
用法:
一、设置cookie
var cookie = new JSCookie();
//普通设置
cookie .SetCookie("key1","val1");
//过期时间为一年
var expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() + 1);
cookie .SetCookie("key2","val2",expire_time);
//设置域及路径,带过期时间
cookie .SetCookie("key3","val3",expire_time,".cnblogs.com","/");
//设置带子键的cookie,子键分别是k1,k2,k3
cookie .SetCookie("key4","k1=1&k2=2&k3=3");
二、读取cookie
//简单获取
cookie .GetCookie("key1");
cookie .GetCookie("key2");
cookie .GetCookie("key3");
cookie .GetCookie("key4");
//获取key4的子键k1值
cookie .GetChild("key4","k1");
三、删除
cookie .Expire("key1");
cookie .Expire("key2");
cookie .Expire("key3");
cookie .Expire("key4");
第二种方法:cookie操作函数,脚本之家也是用的这个。大家可以根据需要选择。
function setCookie(name, value) //cookies设置JS
{
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24));
}
document.cookie = name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +LargeExpDate.toGMTString()));
}
function getCookie(Name) //cookies读取JS
{
var search = Name + "="
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search)
if(offset != -1)
{
offset += search.length
end = document.cookie.indexOf(";", offset)
if(end == -1) end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
else return ""
}
}
使用方法:
if(getCookie("yxjok")!="ok"){
//判断cookie中yxjok的值是不是为ok,不是则显示下面的广告。
document.write('<div id="jb51_yxj"><a href="https://www.jb51.net" onclick="Closeyxj()" target="_blank"><img src="https://www.jb51.net/images/logo.gif"
/></a></div>');
}
function Closeyxj(){
//关闭广告的现实。并用cookies记录已经显示过了,这里的功能主要是关闭后一段时间不显示默认是24小时。
$("jb51_yxj").style.display='none';
setCookie("yxjok","ok",10);
}
function setADCookie(name, value) //主要是修改了cookies的过期时间,为几分钟。
{
var argv = setADCookie.arguments;
var argc = setADCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*300));
}
document.cookie = name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +LargeExpDate.toGMTString()));
}
复制代码 代码如下:
String.prototype.Trim = function()
{
return this.replace(/^\s+/g,"").replace(/\s+$/g,"");
}
function JSCookie()
{
this.GetCookie = function(key)
{
var cookie = document.cookie;
var cookieArray = cookie.split(';');
var getvalue = "";
for(var i = 0;i<cookieArray.length;i++)
{
if(cookieArray[i].Trim().substr(0,key.length) == key)
{
getvalue = cookieArray[i].Trim().substr(key.length + 1);
break;
}
}
return getvalue;
};
this.GetChild = function(cookiekey,childkey)
{
var child = this.GetCookie(cookiekey);
var childs = child.split('&');
var getvalue = "";
for(var i = 0;i < childs.length;i++)
{
if(childs[i].Trim().substr(0,childkey.length) == childkey)
{
getvalue = childs[i].Trim().substr(childkey.length + 1);
break;
}
}
return getvalue;
};
this.SetCookie = function(key,value,expire,domain,path)
{
var cookie = "";
if(key != null && value != null)
cookie += key + "=" + value + ";";
if(expire != null)
cookie += "expires=" + expire.toGMTString() + ";";
if(domain != null)
cookie += "domain=" + domain + ";";
if(path != null)
cookie += "path=" + path + ";";
document.cookie = cookie;
};
this.Expire = function(key)
{
expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() - 1);
var cookie = " " + key + "=e;expires=" + expire_time + ";"
document.cookie = cookie;
}
}
用法:
一、设置cookie
var cookie = new JSCookie();
//普通设置
cookie .SetCookie("key1","val1");
//过期时间为一年
var expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() + 1);
cookie .SetCookie("key2","val2",expire_time);
//设置域及路径,带过期时间
cookie .SetCookie("key3","val3",expire_time,".cnblogs.com","/");
//设置带子键的cookie,子键分别是k1,k2,k3
cookie .SetCookie("key4","k1=1&k2=2&k3=3");
二、读取cookie
//简单获取
cookie .GetCookie("key1");
cookie .GetCookie("key2");
cookie .GetCookie("key3");
cookie .GetCookie("key4");
//获取key4的子键k1值
cookie .GetChild("key4","k1");
三、删除
cookie .Expire("key1");
cookie .Expire("key2");
cookie .Expire("key3");
cookie .Expire("key4");
第二种方法:cookie操作函数,脚本之家也是用的这个。大家可以根据需要选择。
复制代码 代码如下:
function setCookie(name, value) //cookies设置JS
{
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24));
}
document.cookie = name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +LargeExpDate.toGMTString()));
}
function getCookie(Name) //cookies读取JS
{
var search = Name + "="
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search)
if(offset != -1)
{
offset += search.length
end = document.cookie.indexOf(";", offset)
if(end == -1) end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
else return ""
}
}
使用方法:
复制代码 代码如下:
if(getCookie("yxjok")!="ok"){
//判断cookie中yxjok的值是不是为ok,不是则显示下面的广告。
document.write('<div id="jb51_yxj"><a href="https://www.jb51.net" onclick="Closeyxj()" target="_blank"><img src="https://www.jb51.net/images/logo.gif"
/></a></div>');
}
function Closeyxj(){
//关闭广告的现实。并用cookies记录已经显示过了,这里的功能主要是关闭后一段时间不显示默认是24小时。
$("jb51_yxj").style.display='none';
setCookie("yxjok","ok",10);
}
function setADCookie(name, value) //主要是修改了cookies的过期时间,为几分钟。
{
var argv = setADCookie.arguments;
var argc = setADCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*300));
}
document.cookie = name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +LargeExpDate.toGMTString()));
}
您可能感兴趣的文章:
- javascript实现操作cookie实现的可记忆菜单
- javascript操作cookie_获取与修改代码
- javascript 操作cookies及正确使用cookies的属性
- JavaScript 对Cookie 操作的封装小结
- Javascript Cookie读写删除操作的函数
- javascript操作cookie的文章(设置,删除cookies)
- javascript cookies操作集合
- javascript操作cookie方法函数集合
- Javascript操作cookie的函数代码
- 浅析javascript操作 cookie对象
- 基于javascript的COOkie的操作实现只能点一次
- JavaScript中Cookie操作实例
- JavaScript操作Cookie详解
- javascript操作Cookie(设置、读取、删除)方法详解
最新评论