如何使用JavaScript获取指定的Cookie路径 [英] How to get a specified Cookie path using javascript
问题描述
反正有没有在Javascript中获得指定cookie的路径
Is there anyway to get a specified cookie's Path in Javascript ,
我有一个javascript方法,该方法将根据name返回cookie值.是否可以向该方法添加过滤器,使其仅从特定路径中获取Cookie?
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 ?
要求是从名为/ctp的路径中获取Cookie?
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 "";
}
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 "";
}
推荐答案
我认为您正在寻找 manifest.webmanifest
文件,并且有
I think you're looking for cookies.getAll()
, which requires setting up a manifest.webmanifest
file and there's caveats among support issues.
我个人认为localStorage
和/或sessionStorage
是一个更好的选择...很好,如果保持理智是一种选择,否则最好在读取和读取对象时保持值新鲜写入数据.
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.
这篇关于如何使用JavaScript获取指定的Cookie路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!