Code
<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Cookie. aspx. CS " Inherits = " Cookie " %>
<!Doctype HTML public"-// W3C // dtd xhtml 1.0 transitional // en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< HTML xmlns = " Http://www.w3.org/1999/xhtml " >
< Head runat = " Server " >
< Title > No title page < / Title>
< Script Language = " Javascript " Type = " Text/JavaScript " >
/*
Function: saves cookies.
Parameter: name, Cookie name; value, value
*/
Function Setcookie (name, value ){
VaR Days = 60 ; // Cookie will be saved for two months
VaR Exp = New Date (); // Get current time
Exp. settime (exp. gettime () + Days * 24 * 60 * 60 * 1000 ); // Change to millisecond
Document. Cookie = Name + " = " + Escape (value) + " ; Expires = " + Exp. togmtstring ();
}
/*
Function: used to obtain cookies.
Parameter: name, Cookie name
*/
Function Getcookie (name ){
VaR Arr = Document. Cookie. Match ( New Regexp ( " (^ |) " + Name + " = ([^;] *) (; | $) " ));
If (ARR ! = Null )
Return Unescape (ARR [ 2 ]);
Return Null ;
}
/*
Function: delete cookies.
Parameter: name, Cookie name
*/
Function Delcookie (name ){
VaR Exp = New Date (); // Current Time
Exp. settime (exp. gettime () - 1 );
VaR Cval = Getcookie (name );
If (Cval ! = Null ) Document. Cookie = Name + " = " + Cval + " ; Expires = " + Exp. togmtstring ();
}
FunctionAddcookie ()
{
Setcookie ("Myname","Wutao");
}
FunctionReadcookie ()
{
}
< / SCRIPT>
< / Head>
< Body >
< Form ID = " Form1 " Runat = " Server " >
< Div >
< A href = " # " Onclick = " Setcookie ('myname', 'wutao ') " > Add cookie < / A> <br / >
< A href = " # " Onclick = " Alert (getcookie ('myname ')) " > Read cookie < / A>
< / Div>
< / Form>
< / Body>
< / HTML>