Cookie (javascript & Asp.net)

Source: Internet
Author: User
Javascript
1. Create 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. Read 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. Clear the cookie (invalidate the 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. Create 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. Read 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. Delete 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 );
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.