Asp. NET in the cookie notation:
protected void Button1_Click (object sender, EventArgs e)/write Cookie
{
HttpCookie cookie = new HttpCookie ("name");//two names are different; 1: Instantiate a Cookie object (equivalent to the key value pair name is the key cookie is the value)
Cookie. Value = "Marry"; 2: Give the cookie value
Cookie. Expires = DateTime.Now.AddDays (1); The expiration time of the 3:cookie (plus any number of expiration times for any unit at the current time); If you do not set an expiration time cookie is not written to the hard disk, only in memory, when the browser is closed when the memory is recycled cookies are gone; (Temporary cookies)
RESPONSE.COOKIES.ADD (cookie);//4: Real execution put the object in the collection
}
protected void button2_click (object sender, EventArgs e)//Read Cookie
{
if (request.cookies["name"]!= null)//Read
{
TextBox1.Text = request.cookies["Name". value;//cookies["name"] means that the cookie in the definition above is a key-value pair. Value is values
}
Else
{
Textbox1.text= "No Yo!"
}
}
The cookie in JS:
function Writecookie () {
var today = new Date ();
Today.setdate (Today.getdate () + 1);
Document.cookie = ' Name= my song; expires= ' + today.togmtstring ()//Set cookie value and expiration time of cookie in document
Document.cookie = ' username= qu wanting; expires= ' + today.togmtstring ();
Document.cookie = ' password=123;expires= ' + today.togmtstring ();
}
function read ()//reading a value in a 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")//a cookie file if more than one cookie is read in this way, there are no spaces before the first name, and spaces should be added before the name. Use Trim to space
// {
Alert (a[1]);
// }
if (A[0].trim (") = =" username ")//a cookie file if more than one cookie is read in this way, there are no spaces before the first name, and spaces should be added before the name; use trim to space (?????? Is there a problem? What do you mean, you don't support trim? Firefox is on the line IE not possible configuration problem what is the problem?
{
Alert (a[1]);
}