Javascript operation cookie

Source: Internet
Author: User
Tags setcookie
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>

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.