【问题标题】:JQuery Cookie set cookie for specific url issue on IE & EdgeJQuery Cookie 为 IE 和 Edge 上的特定 url 问题设置 cookie
【发布时间】:2018-01-14 23:28:42
【问题描述】:

我正在使用 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/

【问题讨论】:

标签: javascript jquery cookies jquery-cookie


【解决方案1】:

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

关于 Internet Explorer 的注意事项:

由于底层 WinINET InternetGetCookie 中的一个模糊错误 实现,IE 的 document.cookie 不会返回 cookie 使用包含文件名的路径属性设置。

(来自 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);
}  

【讨论】:

    猜你喜欢
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    相关资源
    最近更新 更多