天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

javascript cookie操作類的實現代碼小結附使用方法

第一種方法: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="http://www.jb51.NET" onclick="Closeyxj()" target="_blank"><img src="http://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技術javascript cookie操作類的實現代碼小結附使用方法,轉載需保留來源!

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

主站蜘蛛池模板: 99久久国内精品成人免费 | 成年人免费观看的视频 | 么么哒日本影院 | 亚洲野狼综合网站 | 国产在线观看免费观看不卡 | 草699一码二码三码四码 | FREE性丰满白嫩白嫩的HD | 99E久热只有精品8在线直播 | 午夜福利视频极品国产83 | 国产 亚洲 中文字幕 久久网 | 亚洲国产欧美另类 | 强行撕开衣服捏胸黄文 | 小泽玛丽av无码观看 | 中文字幕 日韩 无码 在线 | 2019中文字幕乱码免费 | 国内精品日本久久久久影院 | 中文日韩亚洲欧美字幕 | 亚洲AV无码专区国产精品99 | 亚洲综合无码一区二区 | 高清毛片一区二区三区 | 中文字幕在线观看网站 | 午夜亚洲精品不卡在线 | 天津相声广播在线收听 | 中文字幕永久在线 | 色多多污污下载 | 久久久久久九九 | 24小时日本在线观看片免费 | 日本中文字幕巨大的乳专区 | 欧美成人一区二免费视频 | 麻豆精品一卡2卡三卡4卡免费观看 | 国产免费久久精品国产传媒 | 在线播放免费人成毛片视频 | 久久99国产精品无码AV | 色欲av蜜臀av高清 | 巨污全肉np一女多男 | 97久久精品人人槡人妻人 | 大胆国模一区二区三区伊人 | 嗯好大好猛皇上好深用力 | 大学生一级毛片免费看 | 美女PK精子小游戏 | 国产电影午夜成年免费视频 |