js怎么写cookie( 二 )

< clen){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("username1",99);alert(GetCookie("username1")); 。
4.怎么用js写cookie值到缓存里,并读取它function GetCookieVal(offset)
//获得Cookie解码后的值
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(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 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" : "");
}
5.如何用js来操作cookie呢js操作COOKIE,直接给document加上cookie就可以了,但是一般如果单个的加会很麻烦所以一般会直接写好一个函数,可以直接操作cookie,这样就很方便了
setCookie这个是写入cookie,第一个是名称,第二个是cookie值,第三个是过期时间
getCookie这个是查找cookie;
removeCookie这是你需要删除的cookie;
function setCookie(name, value, iDay)
{
var oDate=new Date();
oDate.setDate(oDate.getDate()+iDay);
document.cookie=name+'='+encodeURIComponent(value)+';expires='+oDate;
}
function getCookie(name)
{
var arr=document.cookie.split('; ');
var i=0;
for(i=0;i['username', 'abc']
var arr2=arr[i].split('=');
if(arr2[0]==name)
{
var getC = decodeURIComponent(arr2[1]);
return getC;
}
}
return '';
}
function removeCookie(name)
{
setCookie(name, '1', -1);
}
6.怎么用JS定义一个cookies<script>function setCookie(name,value,expires){//写cookievar exp=new Date();exp.setTime(exp.getTime()+expires*60000);document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();//+";domain=arkoo.com;path=/";} function readcookie(name){//读取cookievar oRegex=new RegExp(name+'=([^;]+)','i');var oMatch=oRegex.exec(document.cookie);if(oMatch&&oMatch.length>1)return unescape(oMatch[1]);else return '';}setCookie("arkoo","看看这个怎么样",1000);alert(readcookie("arkoo"));</script> 。

js怎么写cookie

文章插图