繁体   English   中英

jQuery Cookie为IE和Edge上的特定URL问题设置cookie

[英]JQuery Cookie set cookie for specific url issue on IE & Edge

我正在使用JQuery cookie插件,并且试图在用户按下按钮时设置cookie。 同样重要的是,cookie 仅对当前页面有效 ,因此对整个文档无效

这是我到目前为止尝试过的:

if ($.cookie('pressed')) { 
    alert("Button is already pressed.")
} else {
    $.cookie('pressed', 'somevalue', { expires: 1, path: window.location.pathname });
    executeSomeFunction();
}  

上面的代码似乎可以在Chrome上正常运行,但在Edge和IE11上无法运行。 实际上,它甚至没有将cookie保存在上述浏览器中。

我正在使用jquery cookie插件(链接在这里: https : //plugins.jquery.com/cookie/

没关系。 我找到了答案。 显然,IE(和Edge)中存在错误

关于Internet Explorer的注意事项:

由于基础WinINET InternetGetCookie实现中的一个模糊错误,如果IE的document.cookie被设置为包含文件名的路径属性,则该cookie不会返回。

(来自Internet Explorer Cookie内部(FAQ))

这意味着如果这样的路径名包含如下文件名,则无法使用path:window.location.pathname设置路径:/check.html(或者至少不能正确读取此类cookie)。

我通过简单地根据当前页面的URL命名cookie来解决它。 这样,每个页面都有一个唯一的cookie名称。

我这样做是这样的:

if (Cookies.get(window.location.pathname)) { 
    alert("Sie haben dieses Rezept bereits bewertet. Wenn Sie erneut bewerten wollen, können Sie dies nach 24 Stunden tun.")
} else {
    Cookies.set(window.location.pathname, 'value', { expires: 1, path: '' });
    executeRating(star, voteCount, voteAverage, nodeId);
}  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM