Cookie writing in ASP. NET:
Protected void button#click (object sender, EventArgs e) // write cookie
{
HttpCookie cookie = new HttpCookie ("name"); // two names are different; 1: instantiate a cookie object (equivalent to key-value pair name is key-cookie is value)
Cookie. Value = "marry"; // 2: cookie Value
Cookie. expires = DateTime. now. addDays (1); // 3: cookie expiration time (add any number of expiration times per unit to the current time); if no expiration time is set, the cookie will not be written to the hard disk, this memory only exists in the memory. When the browser is closed, the cookie will be recycled. (temporary cookie)
Response. Cookies. Add (cookie); // 4: The object is put into the set.
}
Protected void Button2_Click (object sender, EventArgs e) // read cookie
{
If (Request. Cookies ["name"]! = Null) // read
{
TextBox1.Text = Request. Cookies ["name"]. Value; // Cookies ["name"] indicate that the cookie defined above is a key-value pair. Value.
}
Else
{
TextBox1.Text = "No! ";
}
}
Cookie writing in js:
Function writecookie (){
Var today = new Date ();
Today. setDate (today. getDate () + 1 );
Document. cookie = 'name = My songs; expires = '+ today. toGMTString (); // set the cookie value and cookie expiration time in the document
Document. cookie = 'username = Qu wanting; expires = '+ today. toGMTString ();
Document. cookie = 'password = 123; expires = '+ today. toGMTString ();
}
Function read () // read the value in the cookie
{
// Read multiple
Var cookie = document. cookie;
Var arry = cookie. split (';');
Alert (arry );
For (var I in arry) // different from c #
{
Var a = arry [I]. split ('= ');
// If (a [0] = "username") // if multiple cookies are read in a cookie file using this method, there is no space before the first name, spaces must be added before the names. Use trim to remove spaces.
//{
// Alert (a [1]);
//}
If (a [0]. trim ('') =" username ") // if multiple cookies in a cookie file are read using this method, there is no space before the first name, spaces must be added before the names in the future. Use trim to remove spaces (?????? Is there a problem? How can I say that trim is not supported? Firefox does not support ie and may not support configuration. What is the problem ?)
{
Alert (a [1]);
}