Cookie(javascript & Asp.net)

來源:互聯網
上載者:User
javascript
1.建立cookieCode
 function SetCookie(name,value,expiredays,path,domain,secure)
      {
     
        var expDate=new Date();
        expDate.setDate(expDate.getDate()+expiredays);
        var expString=((expiredays==null)?"" ";expires="+expDate.toGMTString()));
        var pathString=((path==null)?"" ";path="+path));
        var domainString=((domain==null)?"" ";domain="+domain));
        var secureString=((secure==true)?";secure":"");
        document.cookie=name+"="+escape(value)+expString+pathString+domainString+secureString;
       
      }

2.讀取cookieCode
   function GetCookie(name)
      {
        var result=null;
        var myCookie=document.cookie+";";
        var searchName=name+"=";
        var startOfCookie=myCookie.indexOf(searchName);
        var endOfCookie;
        if(startOfCookie>-1)
        {
           startOfCookie += searchName.length;
           endOfCookie=myCookie.indexOf(";",startOfCookie); 
           result=unescape(myCookie.substring(startOfCookie,endOfCookie));
        }
         return result; 
           
      }

3.清除cookie(使cookie失效)Code
  function ClearCookie(name,path,domain)
      {
         var expDate=new Date();
         expDate.setDate(expDate.getDate()-1);
          var pathString=((path==null)?"" ";path="+path));
         document.cookie=name+"=;expires="+expDate.toGMTString()+pathString;
      }

Asp.net
1.建立CookieCode
 public void SetCookie(string name)
    {
       // HttpCookie myCookie = new HttpCookie("UserSettings");
        HttpCookie myCookie = new HttpCookie(name);
        myCookie["Font"] = "Arial";
        myCookie["Color"] = "Blue";
        myCookie.Expires = DateTime.Now.AddDays(1d);
        Response.Cookies.Add(myCookie);
    }

2.讀取CookieCode
public string GetCookie(string name)
    {
        string result = "";
        if (Request.Cookies[name] != null)
        {            
            if (Request.Cookies[name]["Font"] != null)
            { result = Request.Cookies[name]["Font"]; }
           
        }
        return result;
    }

3.刪除CookieCode
 public void DeleteCookie(string name)
    {
      //if (Request.Cookies["UserSettings"] != null)
        if (Request.Cookies[name] != null)
        {
            HttpCookie myCookie = new HttpCookie(name);
            myCookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie);
        }
    
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.