javascript cookie操作類的實(shí)現(xiàn)代碼小結(jié)附使用方法
更新時(shí)間:2010年06月02日 14:32:02 作者:
javascript cookie操作類的實(shí)現(xiàn)代碼小結(jié)附使用方法,對(duì)于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;
}
}
用法:
一、設(shè)置cookie
var cookie = new JSCookie();
//普通設(shè)置
cookie .SetCookie("key1","val1");
//過期時(shí)間為一年
var expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() + 1);
cookie .SetCookie("key2","val2",expire_time);
//設(shè)置域及路徑,帶過期時(shí)間
cookie .SetCookie("key3","val3",expire_time,".cnblogs.com","/");
//設(shè)置帶子鍵的cookie,子鍵分別是k1,k2,k3
cookie .SetCookie("key4","k1=1&k2=2&k3=3");
二、讀取cookie
//簡(jiǎn)單獲取
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操作函數(shù),腳本之家也是用的這個(gè)。大家可以根據(jù)需要選擇。
function setCookie(name, value) //cookies設(shè)置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="http://www.dbjr.com.cn" onclick="Closeyxj()" target="_blank"><img src="http://www.dbjr.com.cn/images/logo.gif"
/></a></div>');
}
function Closeyxj(){
//關(guān)閉廣告的現(xiàn)實(shí)。并用cookies記錄已經(jīng)顯示過了,這里的功能主要是關(guān)閉后一段時(shí)間不顯示默認(rèn)是24小時(shí)。
$("jb51_yxj").style.display='none';
setCookie("yxjok","ok",10);
}
function setADCookie(name, value) //主要是修改了cookies的過期時(shí)間,為幾分鐘。
{
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()));
}
復(fù)制代碼 代碼如下:
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;
}
}
用法:
一、設(shè)置cookie
var cookie = new JSCookie();
//普通設(shè)置
cookie .SetCookie("key1","val1");
//過期時(shí)間為一年
var expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() + 1);
cookie .SetCookie("key2","val2",expire_time);
//設(shè)置域及路徑,帶過期時(shí)間
cookie .SetCookie("key3","val3",expire_time,".cnblogs.com","/");
//設(shè)置帶子鍵的cookie,子鍵分別是k1,k2,k3
cookie .SetCookie("key4","k1=1&k2=2&k3=3");
二、讀取cookie
//簡(jiǎn)單獲取
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操作函數(shù),腳本之家也是用的這個(gè)。大家可以根據(jù)需要選擇。
復(fù)制代碼 代碼如下:
function setCookie(name, value) //cookies設(shè)置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 ""
}
}
使用方法:
復(fù)制代碼 代碼如下:
if(getCookie("yxjok")!="ok"){
//判斷cookie中yxjok的值是不是為ok,不是則顯示下面的廣告。
document.write('<div id="jb51_yxj"><a href="http://www.dbjr.com.cn" onclick="Closeyxj()" target="_blank"><img src="http://www.dbjr.com.cn/images/logo.gif"
/></a></div>');
}
function Closeyxj(){
//關(guān)閉廣告的現(xiàn)實(shí)。并用cookies記錄已經(jīng)顯示過了,這里的功能主要是關(guān)閉后一段時(shí)間不顯示默認(rèn)是24小時(shí)。
$("jb51_yxj").style.display='none';
setCookie("yxjok","ok",10);
}
function setADCookie(name, value) //主要是修改了cookies的過期時(shí)間,為幾分鐘。
{
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實(shí)現(xiàn)操作cookie實(shí)現(xiàn)的可記憶菜單
- javascript操作cookie_獲取與修改代碼
- javascript 操作cookies及正確使用cookies的屬性
- JavaScript 對(duì)Cookie 操作的封裝小結(jié)
- Javascript Cookie讀寫刪除操作的函數(shù)
- javascript操作cookie的文章(設(shè)置,刪除cookies)
- javascript cookies操作集合
- javascript操作cookie方法函數(shù)集合
- Javascript操作cookie的函數(shù)代碼
- 淺析javascript操作 cookie對(duì)象
- 基于javascript的COOkie的操作實(shí)現(xiàn)只能點(diǎn)一次
- JavaScript中Cookie操作實(shí)例
- JavaScript操作Cookie詳解
- javascript操作Cookie(設(shè)置、讀取、刪除)方法詳解
相關(guān)文章
輸入密碼檢測(cè)大寫是否鎖定js實(shí)現(xiàn)代碼
網(wǎng)站登錄為了更好的用戶體驗(yàn)都會(huì)在輸入密碼的時(shí)候檢測(cè)是否開啟大寫,這樣有助于提醒用戶,需要學(xué)習(xí)的朋友可以參考下2012-12-12JS去掉第一個(gè)字符和最后一個(gè)字符的實(shí)現(xiàn)代碼
本篇文章主要是對(duì)JS去掉第一個(gè)字符和最后一個(gè)字符的實(shí)現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-02-02有關(guān)div頁(yè)面拖動(dòng)、縮放、關(guān)閉、遮罩效果代碼
有關(guān)div頁(yè)面拖動(dòng)、縮放、關(guān)閉、遮罩效果代碼,比較不錯(cuò),適合學(xué)習(xí)用。2009-08-08js點(diǎn)擊列表文字對(duì)應(yīng)該行顯示背景顏色的實(shí)現(xiàn)代碼
這篇文章主要介紹了js點(diǎn)擊列表文字對(duì)應(yīng)該行顯示背景顏色的實(shí)現(xiàn)代碼,感興趣的小伙伴可以參考下2015-08-08用javascript動(dòng)態(tài)調(diào)整iframe高度的方法
用javascript動(dòng)態(tài)調(diào)整iframe高度的方法...2007-03-03js采用concat和sort將N個(gè)數(shù)組拼接起來的方法
這篇文章主要介紹了js采用concat和sort將N個(gè)數(shù)組拼接起來的方法,涉及JavaScript針對(duì)數(shù)組的合并與排序操作相關(guān)技巧,需要的朋友可以參考下2016-01-01