function getCookie(a){
var b = "";
var c = a + "=";
var d = documentcookie;
if (dlength > 0) {
g = dindexOf(c);
if (g != -1) {
g += clength;
f = dindexOf(";", g);
if (f == -1)
f = dlength;
b = unescape(dsubstring(g, f));
};
};
return b;
},
用documentcookie 获取当前域名下的cookie。具体代码如下:
var mycookie = documentcookie
它读出来是一串字符串,包含当前域名下的所有cookie,解析字符串可以得到cookie值
documentcookie这个。
// 函数名称: getCookie
// 函数功能: 读取cookie函数
// 入口参数: Name:cookie名称
function getCookie(Name) {
var search = Name + "=" ;
if(documentcookielength > 0) {
offset = documentcookieindexOf(search) ;
if(offset > -1) {
offset += searchlength ;
end = documentcookieindexOf(";", offset)
if(end > -1) {
end = documentcookielength ;
}
return unescape(documentcookiesubstring(offset, end)) ;
} else {
return "" ;
}
}
}
用js向cookie中保存数据、获取数据的方法如下:
function GetCookieVal(offset)
//获得Cookie解码后的值
{
var endstr = documentcookieindexOf (";", offset);
if (endstr == -1)
endstr = documentcookielength;
return unescape(documentcookiesubstring(offset, endstr));
}
//---------------------------
function SetCookie(name, value)
//设定Cookie值
{
var expdate = new Date();
var argv = SetCookiearguments;
var argc = SetCookieargumentslength;
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) expdatesetTime(expdategetTime() + ( expires 1000 ));
documentcookie = name + "=" + escape (value) +((expires == null) "" : ("; expires="+ expdatetoGMTString()))
+((path == null) "" : ("; path=" + path)) +((domain == null) "" : ("; domain=" + domain))
+((secure == true) "; secure" : "");
}
//---------------------------------
function DelCookie(name)
//删除Cookie
{
var exp = new Date();
expsetTime (expgetTime() - 1);
var cval = GetCookie (name);
documentcookie = name + "=" + cval + "; expires="+ exptoGMTString();
}
//------------------------------------
function GetCookie(name)
//获得Cookie的原始值
{
var arg = name + "=";
var alen = arglength;
var clen = documentcookielength;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (documentcookiesubstring(i, j) == arg)
return GetCookieVal (j);
i = documentcookieindexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
SetCookie("username1",99);
alert(GetCookie("username1"));
这里的SetCookie 是自己定义的一个函数
如:
function setCookie(name, value, expire) {
windowdocumentcookie = name + "=" + escape(value) + ((expire == null) "" : ("; expires=" + expiretoGMTString()));
}
所以里面的这些参数都是直接定义的JS只有windowdocumentcookie 定义cookie的方法至于里面的值 直接定义
第一种:用原生的documentcookie读取cookie
//读取cookies
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;])(;|$)");
if(arr=documentcookiematch(reg))
return unescape(arr[2]);
else
return null;
}
使用示例:
getCookie("name")
第二种是用 jquerycookie js *** 作cookie,读取cookie如下:
$cookie('name')
这样写
<script>
var page = <%= requestgetParameter("page") %> ;
if (page == 2) {
var istz = getCookie("istz"); //获取cookie
if (istz == null) { //如果cookie为null
setCookie("istz", "yesTz", 10); //设置cookie
locationhref = "hhtml"; //跳转到指定链接
}
}
//设置cookie
function setCookie(name, value, expiredays) {
var ExpireDate = new Date();
ExpireDatesetTime(ExpireDategetTime() + (expiredays 60 1000));
documentcookie = name + "=" + escape(value) + ((expiredays == null) "" : "; expires=" + ExpireDatetoGMTString());
}
//获取cookie
function getCookie(name) {
var strCookie = documentcookie;
var arrCookie = strCookiesplit(";");
for (var i = 0; i < arrCookielength; i++) {
var arr = arrCookie[i]split("=");
if (arr[0] == name) return arr[1];
}
return null;
}
</script>
以上就是关于关于js读取cookie全部的内容,包括:关于js读取cookie、js中如何获取cookies中的键值的值、用java创建的cookie,在页面上用js怎么可以获取呀!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)