Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE8 blocking JavaScript Cookies

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

like image 466
schalkneethling Avatar asked Jun 01 '10 19:06

schalkneethling


2 Answers

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.

like image 122
EvilDuck Avatar answered Oct 13 '22 10:10

EvilDuck


One problem may be that you're using getDate(), which returns the day of the month. If your expiredays 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:

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());}
like image 27
Andrew Avatar answered Oct 13 '22 10:10

Andrew