问题
Is there anyway to get a specified cookie's Path in Javascript ,
i have a javascript method which will return cookie value based on the name . Is it possible to add filter to the method such that it will fetch from particular paths cookies only ?
requirement is to fetch cookie from a path named /ctp ?
function getCookie(cname) {
var name = cname + "=";
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);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
回答1:
I think you're looking for cookies.getAll(), which requires setting up a manifest.webmanifest file and there's caveats among support issues.
Personally I think localStorage and/or sessionStorage are a far better choice... well if keeping one's sanity is an option, otherwise best of luck keeping values fresh across objects reading and writing data.
来源:https://stackoverflow.com/questions/38026119/how-to-get-a-specified-cookie-path-using-javascript