开发者

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

开发者 https://www.devze.com 2023-02-23 00:57 出处:网络
The following .Net settings set the cookie HttpCookie c = new HttpCookie(\"tw\"); c.Expires = DateTime.Now.AddDays(100);

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消