Cookie always return null in IE

I'm debugging a script, that basically reads a cookie and returns some stuff. It works fine in all browsers, except for IE. After some testing, I discover that it never enters the for loop.

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');

  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);

    if (c.indexOf(nameEQ) == 0) {   
        return c.substring(nameEQ.length,c.length);
    }

  }
  return null;
}

After some more testing, I realize the variable ca, which store the cookie, never even gets defined. Looking at it, I don't see why it should, as there is no reference to the cookiename. So in my logic, it shouldn't even work, yet it works perfectly in everything but IE.

var ca = document.cookie.split(';');  

1: Why does this even work in Fx, Webkit and Opera? 2: How can I make it work in IE as well?

I tried defining it as below, but that didn't seem to work:

document.cookie = name;

Answers


It appears there are no cookies set in IE. Try using

alert('d.cookie:\t'+document.cookie+'\n\n'+'d.cookie.length:\t'+document.cookie.length);

to check if there are any cookies.

Bookmarklet

If there are none, try setting one using

document.cookie='mycookie=foo; path=/';

Bookmarklet


Need Your Help

Typing Text Animation

javascript jquery html css

Just wondering if anyone could help with an issue I am having.

Abuse yield to avoid condition in loop

python performance generator yield

I need to search for the first, last, any, or all occurence of something in something else. To avoid repeating myself (DRY) I came up with the following solution.