bizl
bizl

Reputation: 1563

cookies set in .Net read clientside in Firefox but not IE or Safari

The following .Net settings set the cookie

                HttpCookie c = new HttpCookie("tw"); 
                c.Expires = DateTime.Now.AddDays(100);
                c.Path = "/"; 
                c.Secure = false;  
                c.HttpOnly = false;

The following javascript reads the cookies

    function getCookie(c_name)
    {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
        {
        return unescape(y);
        }
      } 
}

No extra headers are sent in the request. This works in Firefox but not IE or Safari

Upvotes: 1

Views: 658

Answers (1)

Genady Sergeev
Genady Sergeev

Reputation: 1650

What happens here is that you don't set value to your cookie. In IE, valueless cookie doesn't contain '='. This means, that in your code x is "" and y is "tw". Then, in the if clause, the name comparison always fail, because x is an empty string. As a result, the function returns undefinied. Rewrite your logic in a way that handles the absence of '=' in a valueless cookie in IE.

Upvotes: 1

Related Questions