简体   繁体   English

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

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

I'm using the JQuery cookie plugin & I'm trying to set a cookie if a user presses a button. 我正在使用JQuery cookie插件,并且试图在用户按下按钮时设置cookie。 It's also very important, that the cookie is only valid for the current page and therefore not for the whole document. 同样重要的是,cookie 仅对当前页面有效 ,因此对整个文档无效

This is what I tried so far: 这是我到目前为止尝试过的:

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

The code above seems to work fine for Chrome but fails on edge and IE11. 上面的代码似乎可以在Chrome上正常运行,但在Edge和IE11上无法运行。 In fact, it doesn't even save the cookie in the mentioned browsers. 实际上,它甚至没有将cookie保存在上述浏览器中。

I'm using the jquery cookie plugin (link here: https://plugins.jquery.com/cookie/ ) 我正在使用jquery cookie插件(链接在这里: https : //plugins.jquery.com/cookie/

Nevermind. 没关系。 I found the answer. 我找到了答案。 Apparently there is a bug in IE (and Edge) 显然,IE(和Edge)中存在错误

Note regarding Internet Explorer: 关于Internet Explorer的注意事项:

Due to an obscure bug in the underlying WinINET InternetGetCookie implementation, IE's document.cookie will not return a cookie if it was set with a path attribute containing a filename. 由于基础WinINET InternetGetCookie实现中的一个模糊错误,如果IE的document.cookie被设置为包含文件名的路径属性,则该cookie不会返回。

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

This means one cannot set a path using path: window.location.pathname in case such pathname contains a filename like so: /check.html (or at least, such cookie cannot be read correctly). 这意味着如果这样的路径名包含如下文件名,则无法使用path:window.location.pathname设置路径:/check.html(或者至少不能正确读取此类cookie)。

I fixed it by simply naming the cookie according to the current page url. 我通过简单地根据当前页面的URL命名cookie来解决它。 That way there is a unique cookie name for each page. 这样,每个页面都有一个唯一的cookie名称。

I did it like this: 我这样做是这样的:

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