IE8 blocks JavaScript Cookies

Here is one that throws me for a loop. I am trying to set a simple cookie that has one name: a pair of values ​​in IE8. Tested on FF and it works great. IE8 blocks it.

I read about the P3P materials and created a basic P3P document, 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 is blocking me from setting this cookie?

Thanks Schalk

+6
javascript cookies internet-explorer-8 blocking
source share
6 answers

I had the same problem and spent a lot of time figuring out why IE would not save my JS cookie. My P3P file was fine and IE was saving response files, but not JS.

Unexpectedly and, most surprisingly, I found a solution by removing the following line from html:

<meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" / ">

I have no idea why this is happening, but it solved all my problems. Hope this helps someone.

+2
source share

One problem may be that you are using getDate() , which returns the day of the month. If your expiredays too large, it should move on to the next month, but in IE it can stay this month and expire immediately. Perhaps try the following:

 function setCompatibilityCookie(c_name, value, expiredays) { var exdate = new Date(); exdate.setTime(exdate.getTime() + (expiredays * 86400000)); document.cookie= c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toUTCString());} 
+1
source share

I have been using the same code for centuries to set client-side cookies without any problems. I would definitely research setting IE instead of the code itself. In IE, you have a great opportunity to say whether you accept cookies or not depending on the source (as you noticed). I would definitely start here! Good luck.

+1
source share

I experienced this and tried some of the answers here, my released ones turned out to be expiration. I installed it for 99999999999, but when I fell to 9999999, it worked. IE8 seems to have an expiration limit (Microsoft genius, pure genius: s)

0
source share

I faced the same situation, and the problem is that ... expiredays is such a keyword ONLY for IE. If you change the name of the expiredays variable to whateveryouwant , it works well in all browsers.

0
source share

I ran into this problem and found out that it is related to the length of the cookie name. In this case, I had a cookie name that was 26 characters, and this works fine, but IE 8 and below. I reduced the name to 10 characters and all IE 8 read / wrote cookies very well. I assume that the arbitrary limit here is 16 characters for the cookie name IE 8.

0
source share

All Articles