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

來源:互聯網
上載者:User

第一種方法: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()));
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.