views:

494

answers:

2

Hey there folks,

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

A: 

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());}
Andrew
Hi Andres, thanks for the suggestion, I have tried it but the problem still persists. Please see my comment on Philippe's question above.
ossreleasefeed
Ok, the reason why chrome was 'blocking' my cookie was because of this issue http://code.google.com/p/chromium/issues/detail?id=535, you can read more about how to resolve this over here: http://stackoverflow.com/questions/335244/why-does-chrome-ignore-local-jquery-cookies : Now it is just IE8, what a surprise ;)
ossreleasefeed
So your page was being loaded off the local file system and not off a remote server? I think this is your IE problem as well. I know I've encountered this before, years and years ago. Basically, whenever you're loading a file off a local resource (your hard drive or a local share) the browser behaves completely differently. In IE you used to be able to do nearly anything you wanted locally (HTAs were amazing). Then the security model changed and the party was over. Are you developing an app that is meant to run locally or are you just using your local drive for development and testing?
Andrew
Hey Andrew, I experienced the problems initially running a page of a server. But I then isolated the problem and tested of a local file. I have looked at the source of the jQuery Cookie plugin to see how it was done and it seems I might have found the problem.Still need to test it as I think IE8 might allow setting cookies from a local file but not reading, not entirely sure. Will let you know as soon as I have tested it on the server.
ossreleasefeed
Ok, I tried it on the server now.... No luck :( Might running from an IP as apposed to a URL make a difference here? It seems that other cookies are being blocked even before I attempt to set this one.So now I wonder whether there are different restriction placed when running from an IP as apposed to a specific domain. It seems IE8 is placing the IP in the list of restricted sites by default.Does this ring a bell for anyone?
ossreleasefeed
IP vs domain shouldn't make a difference. What happens if you drop the p3p declarations? After setting the cookie but immediately before interacting with the server, does the document.cookie value reflect your changes? After setting the cookie and then interacting with the server, does document.cookie change at all? Are you able to capture the raw HTTP cookie value on the server side? If not, try running it through Fiddler2 to see what the HTTP request headers look like. You may have to also specify the domain and maybe the path in your cookie. Could there be another process writing cookies?
Andrew
+1  A: 

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

Zaziffic