尚方寶劍之說
2019-06-28 16:30:30
按名字獲得曲奇我要從餅干中得到價值?,F(xiàn)在我有兩個名字的餅干shares=名字obligations= .我只想讓這個getter得到義務cookie中的值。我該怎么做?所以for將數(shù)據分割成單獨的值,并將其放入數(shù)組中。 function getCookie1() {
// What do I have to add here to look only in the "obligations=" cookie?
// Because now it searches all the cookies.
var elements = document.cookie.split('=');
var obligations= elements[1].split('%');
for (var i = 0; i < obligations.length - 1; i++) {
var tmp = obligations[i].split('$');
addProduct1(tmp[0], tmp[1], tmp[2], tmp[3]);
}
}
3 回答

呼啦一陣風
TA貢獻1802條經驗 獲得超6個贊
function getCookie(name) { var value = "; " + document.cookie; var parts = value.split("; " + name + "="); if (parts.length == 2) return parts.pop().split(";").shift();}
漫游
"{name}={value}; {name}={value}; ..."
"; {name}={value}; {name}={value}; ..."

鳳凰求蠱
TA貢獻1825條經驗 獲得超4個贊
我更喜歡在cookie上使用一個正則表達式匹配:
window.getCookie?=?function(name)?{ ??var?match?=?document.cookie.match(new?RegExp('(^|?)'?+?name?+?'=([^;]+)')); ??if?(match)?return?match[2];}
或者我們也可以使用作為一個函數(shù),檢查下面的代碼。
function?check_cookie_name(name)? ????{ ??????var?match?=?document.cookie.match(new?RegExp('(^|?)'?+?name?+?'=([^;]+)')); ??????if?(match)?{ ????????console.log(match[2]); ??????} ??????else{ ???????????console.log('--something?went?wrong---'); ??????} ???}

哆啦的時光機
TA貢獻1779條經驗 獲得超6個贊
使用cookie獲取腳本:
function?readCookie(name)?{ ????var?nameEQ?=?name?+?"="; ????var?ca?=?document.cookie.split(';'); ????for(var?i=0;i?<?ca.length;i++)?{ ????????var?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;}
那就稱之為:
var?value?=?readCookie('obligations');
添加回答
舉報
0/150
提交
取消