js cookie 取得多个值 下面这段代码是在多个cookie中取得其中一个,我的问题是,怎样同时取得两个或以上的值?谢谢!
<script language="JavaScript" type="text/javascript">
<!--
//设置两个cookie
document.cookie="userId=828";
document.cookie="userName=hulk";
//获取cookie字符串
var strCookie=document.cookie;
//将多cookie切割为多个名/值对
var arrCookie=strCookie.split("; ");
var userId;
//遍历cookie数组,处理每个cookie对
for(var i=0;i<arrCookie.length;i++){
var arr=arrCookie[i].split("=");
//找到名称为userId的cookie,并返回它的值
if("userId"==arr[0]){
userId=arr[1];
break;
}
}
alert(userId);
//-->
</script>
------解决方案--------------------
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
var cookieNames = {
'name1' : '',
'name2' : ''
}
for ( var i = 0; i < arrCookie.length; i++) {
var arr = arrCookie[i].split("=");
if (cookieNames.hasOwnProperty(arr[0])) {
cookieNames[arr[0]] = arr[1];
}
}
console.log(cookieNames);