js 如何读取cookie?
发布网友
发布时间:2022-04-21 05:35
我来回答
共3个回答
热心网友
时间:2023-11-06 05:50
1、获取所有cookie
function getCookies() {
var showAllCookie = '';
if(!document.cookie == ''){
var arrCookie = document.cookie.split('; '); //用spilt('; ')切割所有cookie保存在数组arrCookie中
var arrLength = arrCookie.length;
for(var i=0; i<arrLength; i++) {
showAllCookie += 'c_name:' + unescape(arrCookie[i].split('=')[0]) + 'c_value:' + unescape(arrCookie[i].split('=')[1]) + '<br>' 9
}
return showAllCookie;
}
}
2、通过名称读取cookie
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
热心网友
时间:2023-11-06 05:50
用document.cookie, 获取到一个字符串:cookie名1=值1;cookie名=值2alert(document.cookie)试试
热心网友
时间:2023-11-06 05:51
function SetCookie(sName, sValue)
{
document.cookie = sName + "=" + escape(sValue) ;
}function GetCookie(sName)
{
// cookies are separated by semicolons
var aCookie = document.cookie.split("; ");
//alert(document.cookie);
for (var i=0; i < aCookie.length; i++)
{
// a name/value pair (a crumb) is separated by an equal sign
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
} // a cookie with the requested name does not exist
return "";
}