これは私があなたに与えることができる最も簡単な方法かもしれません。
クック値を返す関数を呼び出します。
/**
* Returns cookie.
* @param {String} sName Main cookie name.
* @param {String} SubName Sub cookie name.
* @return {String} Cookie.
*/
GetCookie: function (sName, SubName) {
var aCookie = document.cookie.split("; "); // cookies are separated by semicolons
var CookieVal = null;
var bHasKeys;
for (var i = 0; i < aCookie.length; i++) {
var aCrumb = aCookie[i].split("=");
bHasKeys = aCrumb.length > 2 ? 1 : 0;
if (sName == aCrumb[0]) {
var TempVal = aCookie[i];
TempVal = TempVal.substring(TempVal.indexOf(sName) + sName.length + 1, TempVal.length);
if (SubName) {
TempArr = TempVal.split("&"); // subcookie seperated by &
for (var j = 0; j < TempArr.length; j++) {
if (TempArr[j].split("=")[0] == SubName) {
CookieVal = TempArr[j].split("=")[1];
//while loop is added since /+/g syantax does not work here
while (CookieVal.indexOf("+") != -1) CookieVal = CookieVal.replace('+', ' ');
CookieVal = unescape(CookieVal);
break;
}
}
}
else {
if (bHasKeys) CookieVal = TempVal;
else {
CookieVal = aCrumb[1];
//while loop is added since /+/g syantax does not work here
while (CookieVal.indexOf("+") != -1) CookieVal = CookieVal.replace('+', ' ');
CookieVal = unescape(CookieVal);
}
break;
}
}
}
return CookieVal == null ? '' : CookieVal;
}