Asp. The basic usage of cookies in net and JS

Source: Internet
Author: User
Tags current time set cookie split trim

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]);

}

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.