首頁  >  文章  >  web前端  >  js讀寫(刪除)Cookie實例詳解_javascript技巧

js讀寫(刪除)Cookie實例詳解_javascript技巧

WBOY
WBOY原創
2016-05-16 17:36:331284瀏覽
複製代碼 代碼如下:

//JS操作cookies方法!
//寫cookies /寫cookies
function setCookie(name,value)
{
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() Days*24*60* 60*1000);
document.cookie = name "=" escape (value) ";expires=" exp.toGMTString();
}
//讀取cookies
function getCookie(name )
{
var arr,reg=new RegExp("(^| )" name "=([^;]*)(;|$)");
if(arr=document.cookie .match(reg)) return unescape(arr[2]);
else return null;
}
//刪除cookies
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name "= " cval ";expires=" exp.toGMTString();
}
//使用範例
setCookie("name","hayden");
alert(getCookie("name")) ;
//如果需要設定自訂過期時間
//那麼把上面的setCookie 函數換成下面兩個函數就ok;
//程式碼
function setCookie2(name,value ,time){
var strsec = getsec(time);
var exp = new Date();
exp.setTime(exp.getTime() strsec*1);
document.cookie = name "=" escape (value) ";expires=" exp.toGMTString();
}
function getsec(str){
alert(str);
var str1=str.substring( 1,str.length)*1;
var str2=str.substring(0,1);
if (str2=="s"){
return str1*1000;
}else if (str2=="h"){
return str1*60*60*1000;
}else if (str2=="d"){
return str1*24*60*60*1000 ;
}
}
//這是有設定過期時間的使用範例:
//s20是代表20秒
//h是指小時,如12小時則是:h12
//d是天數,30天則:d30
//暫時只寫了這三種
setCookie2("name2","hayden2","s20");
alert (getCookie("name2"));

以下是幾個比較常見的好用的函數:
複製程式碼 程式碼如下:

function GetCookieVal(offset)
//取得Cookie解碼後的值
{
var endstr = document.cookie.indexf ";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
document.cookie.substring(offset, endstr));
}
function SetCookie(name, value)
//設定Cookie值
{
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domainc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if(expires!=null) expdate.setTime(expdate.getTime () ( expires * 1000 ));
document.cookie = name "=" escape (value) ((expires == null) ? "" : ("; expires=" expdate.toGMTString()))
((path == null) ? "" : ("; path=" path)) ((domain == null) ? "" : ("; domain=" domain))
((secure == true ) ? "; secure" : "");
}
function DelCookie(name)
//刪除Cookie
{
var exp = new Date();
exp. setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name "=" cval "; expires=" exp.toGMTString();
}
function GetCookie(name)
//取得Cookie的原始值
{
var arg = name "=";
var alen = arg.length;
var clen = document. cookie.length;
var i = 0;
while (i {
var j = i alen;
if (document.cookie.substring(i, j) = = arg)
return GetCookieVal (j);
i = document.cookie.indexOf(" ", i) 1;
if (i == 0) break;
}
return null;
}
//測試
SetCookie("sunshine","1986");
alert(GetCookie("sunshine"));
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn