IE8 阻止 JavaScript Cookie
这是一个让我大吃一惊的问题。我正在尝试在 IE8 上设置一个具有一个名称:值对的简单 cookie。在FF上测试过,效果很好。 IE8一直阻止它。
我已经阅读了有关 P3P 的内容并创建了一个基本的 P3P 文档,IBM 工具没有报告任何错误,并在所有页面上添加了以下内容:
<meta http-equiv="P3P" CP="CAO DSP COR PSDa CONi TELi OUR STP COM NAV"><link rel="P3Pv1" href="/w3c/p3p.xml"></link>
我用来设置 cookie 的代码如下:
function setCompatibilityCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie= c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toUTCString());}
任何想法为什么 IE8 不断阻止我设置这个cookie?
谢谢你, 沙尔克
Here is one that is throwing me for a loop. I am trying to set a simple cookie that has one name:value pair on IE8. Tested on FF and it works fine. IE8 keeps blocking it.
I have read about the P3P stuff and created a basic P3P doc, no errors reported by the IBM tool, and added the following on all pages:
<meta http-equiv="P3P" CP="CAO DSP COR PSDa CONi TELi OUR STP COM NAV"><link rel="P3Pv1" href="/w3c/p3p.xml"></link>
The code I use to set the cookie is as follows:
function setCompatibilityCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie= c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toUTCString());}
Any ideas why IE8 keeps blocking me from setting this cookie?
Thank you,
Schalk
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我也遇到了同样的问题,并花了很多时间研究为什么 IE 不保存我的 JS cookie。我的 P3P 东西没问题,IE 保存了响应 cookie,但 JS 不行。
突然间,最令人惊讶的是,我通过从 html 中删除以下行找到了解决方案:
<元 http-equiv="Content-Type" content="text/html; charset=utf-8"/ >
我不知道为什么会发生这种情况,但这解决了我所有的问题。希望这对某人有帮助。
I had the same problem and spent a lot of time digging why IE wont save my JS cookie. My P3P stuff was OK and IE was saving response cookies, but not JS.
Suddenly and most surprisingly I found the solution by removing the following line from html:
< meta http-equiv="Content-Type" content="text/html; charset=utf-8"/ >
I have no idea why that happens but that solved all my problems. Hope this helps someone.
一个问题可能是您使用的是
getDate()
,它返回月份中的某一天。如果您的expiredays
太大,它应该滚动到下个月,但在 IE 中,它可能会停留在本月并立即到期。也许试试这个:One problem may be that you're using
getDate()
, which returns the day of the month. If yourexpiredays
is too great, it should roll over to next month, but in IE it may be staying in this month and expiring right away. Maybe try this:我多年来一直使用相同的代码在客户端设置 cookie,没有任何问题。我肯定会调查 IE 设置而不是代码本身。在 IE 中,您有很多可能性来决定是否接受 cookie,具体取决于来源(正如您所发现的那样)。我肯定会从这里开始!祝你好运
I have been using the same code for ages for setting cookies on the client side with no problem whatsoever. I would definitely investigate on IE setup instead of the code itself. In IE, you have a big bunch of possibilities to say if you accept cookies or not depending on the source (as you spotted it). I would definitely start here! Good luck
我经历过这种情况并尝试了这里的一些响应,我的问题结果是过期了。我将其设置为 99999999999,但是当我将其降低到 9999999 时它就起作用了。看来 IE8 的有效期有限制(微软天才,纯粹的天才:s)
I experienced this and tried some of the responses here, my issued turned out to be the expiry. I'd set it for 99999999999, but when I dropped this down to 9999999 it worked. It seems IE8 has a limit on the expiry (genius Microsoft, pure genius :s)
我也遇到过同样的情况,问题是...
expiredays
是仅适用于 IE 的关键字。如果您更改whateveryouwant
中的expiredays
变量的名称,它可以在所有浏览器上正常运行。I've faced the same situation and the problem was that...
expiredays
is such a keyword ONLY for IE. If you change the name ofexpiredays
variable inwhateveryouwant
, it works well on all browsers.我也遇到了这个问题,事实证明这与 cookie 名称的长度有关。在本例中,我有一个 26 个字符的 cookie 名称,除了 IE 8 及更低版本之外,它在任何其他版本中都可以正常工作。我将名称缩短为只有 10 个字符,突然 IE 8 可以正常读取/写入 cookie。我假设 IE 8 cookie 名称的任意限制是 16 个字符。
I ran in to this issue as well, and it turns out it had to do with the length of the cookie name. In this case I had a cookie name that was 26 characters and this works fine in anything but IE 8 and below. I shortened the name to only have 10 characters and all of a sudden IE 8 was reading/writing the cookie just fine. I am assuming the arbitrary limit here is 16 characters for an IE 8 cookie name.