javascript cookie未返回该值
我有一个cookie,该cookie由该函数设置(page calendar.html): 之后,用户被重定向到account.php,这样他就可以登录并在该页面上使用id,这就是我在account.php上的内容:javascript cookie未返回该值,javascript,cookies,jquery-cookie,Javascript,Cookies,Jquery Cookie,我有一个cookie,该cookie由该函数设置(page calendar.html): 之后,用户被重定向到account.php,这样他就可以登录并在该页面上使用id,这就是我在account.php上的内容: function getCookieValue(key) { currentcookie = document.cookie; if (currentcookie.length > 0) { firstidx = currentcook
function getCookieValue(key)
{
currentcookie = document.cookie;
if (currentcookie.length > 0)
{
firstidx = currentcookie.indexOf(key + "=");
if (firstidx != -1)
{
firstidx = firstidx + key.length + 1;
lastidx = currentcookie.indexOf(";",firstidx);
if (lastidx == -1)
{
lastidx = currentcookie.length;
}
return unescape(currentcookie.substring(firstidx, lastidx));
}
}
return "";
}
alert(getCookieValue("eventid"));
难题是:当我刷新calendar.html时,警报返回51,例如,这是该事件的值,但当我刷新account.php时,警报显示:NULL
我尝试了一个jQuerycookie插件,同样的结果,所以我回到了基本的javascript,这让我发疯,我不知道如何传递那个值。
我认为使用cookies是正确的方法,但它不起作用。有什么建议吗?我有一个javascript调试器,我的代码没有错误。为什么不使用GET querystring参数传递值?因为如果他们没有登录…account.php会将他们重定向到index.php。然后他们必须注册,然后他们必须再次登录。因此,我必须通过不太干净的url传递它4到5次。可能只是cookie路径的问题-如果两个页面位于不同的路径下,cookie可能在页面之间不“可见”。解决方案:显式设置路径,以便允许浏览器为URL路径提供cookie。使用类似文档的路径设置cookie。cookie='eventid'+“=”+id+”;path=/”;这样所有人都可以看到cookiepages@Amit我这样做已经不行了,我刚才又试了一次,因为你提到了,什么也没有
function getCookieValue(key)
{
currentcookie = document.cookie;
if (currentcookie.length > 0)
{
firstidx = currentcookie.indexOf(key + "=");
if (firstidx != -1)
{
firstidx = firstidx + key.length + 1;
lastidx = currentcookie.indexOf(";",firstidx);
if (lastidx == -1)
{
lastidx = currentcookie.length;
}
return unescape(currentcookie.substring(firstidx, lastidx));
}
}
return "";
}
alert(getCookieValue("eventid"));