JQuery Cookie 针对 IE & Edge 上的特定 url 问题设置 cookie



我正在使用JQuery cookie插件,如果用户按下按钮,我正在尝试设置cookie。同样非常重要的一点是,cookie 仅对当前页面有效,因此对整个文档无效。

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

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

上面的代码似乎适用于Chrome,但在边缘和IE11上失败。事实上,它甚至没有在提到的浏览器中保存cookie。

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

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

关于IE浏览器的注意事项:

由于底层WinINET InternetGetCookie中的一个晦涩错误 实现,IE的document.cookie不会返回cookie,如果它 使用包含文件名的 path 属性进行设置。

(来自 Internet Explorer Cookie Internals (FAQ((

这意味着不能使用路径设置路径: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);
}  

最新更新