这对你有用。它只是在 cookie 上循环查找您传入的名称。
function getCookieByName(name) {
var cookies = document.cookie.split(";"),
yerCookie = null,
cookiePair;
for (var i=0; i < cookies.length; i++) {
cookiePair = cookies[i].split("=");
if (cookiePair[0].trim() === name) {
yerCookie = cookiePair.slice(1).join("=");
break;
}
}
return yerCookie;
}
或者您可以使用从这里复制的这个不错的功能:
function readCookie(name) {
var nameEQ = name + "=",
ca = document.cookie.split(';'),
i, c;
for (i=0; i < ca.length; i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return null;
}
获得 ecm cookie 后,使用以下内容对其进行解析:
function getECMCookieProperty(ecmCookie, prop) {
if (!ecmCookie) {
return undefined;
}
var props = ecmCookie.split("&"),
result = null,
propList;
for (var i=0;i<props.length;i++){
propList = props[i].split("=");
if (propList[0] === prop) {
result = unescape(propList[1]);
break;
}
}
return result;
}
要使用这些,请执行以下操作:
var ecmCookie = getCookieByName("ecm");
var displayFullName = getECMCookieProperty(ecmCookie, "DisplayFullName");
alert(displayFullName)