我正在尝试使用以下代码获取存在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);
console.log(c);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
我有cookie名称,但我总是得到NULL。
我在开发者工具(https://developers.google.com/web/tools/chrome-devtools/manage-data/cookies)
中看到了Cookie但document.cookie
为""
答案 0 :(得分:0)
试试这个
function readCookie(name){
var nameEQ = name+"=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
if (c.indexOf(nameEQ) !=-1) return c.substring(nameEQ.length,c.length);
}
return null;
}
document.cookie="name=sagar;expire=";
console.log(readCookie(`name`));