一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

javascript cookie操作類的實(shí)現(xiàn)代碼小結(jié)附使用方法

第一種方法:cookie操作類,代碼封裝了,下面也有使用方法,大家可以參考下。
復(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");
//過期時間為一年
var expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() + 1);
cookie .SetCookie("key2","val2",expire_time);
//設(shè)置域及路徑,帶過期時間
cookie .SetCookie("key3","val3",expire_time,".cnblogs.com","/");
//設(shè)置帶子鍵的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操作函數(shù),腳本之家也是用的這個。大家可以根據(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.jb51.NET" onclick="Closeyxj()" target="_blank"><img src="http://www.jb51.NET/images/logo.gif"
/></a></div>');
}
function Closeyxj(){
//關(guān)閉廣告的現(xiàn)實(shí)。并用cookies記錄已經(jīng)顯示過了,這里的功能主要是關(guān)閉后一段時間不顯示默認(rèn)是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技術(shù)javascript cookie操作類的實(shí)現(xiàn)代碼小結(jié)附使用方法,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 成人国产在线视频 | 日韩美女拍拍免费视频网站 | 久久99精品久久只有精品 | 粉嫩粉嫩毛片视频 | 精品无人区一区二区三区 | 国产特黄一级毛片特黄 | 国产成人精品一区二区三区 | 中文字幕日韩一区二区三区不 | 欧美成人精品福利在线视频 | 国产视频 一区二区 | 亚洲精品手机在线 | 国产大臿蕉香蕉大视频 | 免费韩国美女爽快一级毛片 | 国产免费久久精品99久久 | 国产成人黄网址在线视频 | 久久er国产精品免费观看1 | 欧美三级成人观看 | 中文字幕久久网 | 亚洲特一级毛片 | 国产一区二区免费不卡在线播放 | 国产精品青草久久久久福利99 | 中国日韩欧美中文日韩欧美色 | 日本精品一二三区 | 国产精品第九页 | 日本久久精品免视看国产成人 | 国产123| 色丫丫| 国内xxxx乱子另类 | 一本色道久久综合亚洲精品高清 | 国产精品自在自线免费观看 | 91精品综合 | 日本一区二区三区中文字幕 | 国产高清视频网站 | 激情五月综合网 | 91精品国产91久久久久久 | 国产福利精品一区二区 | 色老板最新网站视频地址 | 最新亚洲情黄在线网站 | 久久久不卡国产精品一区二区 | 狠狠做五月深爱婷婷天天综合 | 最新欧美精品一区二区三区不卡 |