获取Cookie函数:
function getCookie(cookieName) {var strCookie = document.cookie
var arrCookie = strCookie.split(" ")
for(var i = 0 i < arrCookie.length i++){
var arr = arrCookie[i].split("=")
if(cookieName == arr[0]){
return arr[1]
}
}
return ""
}
调用:
var user_id = getCookie("selected_prj_id")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
}