Javascript Summary-cookie

Source: Internet
Author: User
Javascript Summary-cookie

Cookies in javascript can be used to save multiple pieces of information, and each item is stored in the following format: Information name = Information Content

The cookie contains some control fields, such as expires, which indicates the cookie expiration time.
Today, I have summarized the cookie operations. All the source code is as follows. I will not elaborate on them one by one when I get off work.
/**//*********************************** ******
Obtain the cookie value based on the name.
**************************************** ***/
Function getcookie (name )...{
VaR value = Document. Cookie;
VaR arr1 = value. Split (";");
For (I = 0; I <arr1.length; I ++ )...{
If (value. Length = 0 )...{
Break;
}
Sname = arr1 [I]. Split ("=") [0];
If (sname = Name )...{
Return arr1 [I]. Split ("=") [1];
}
}
Return NULL;
}
/**//*********************************** ******
Set cookie content
**************************************** ***/
Function setcookie (sname, svalue)... {// create a cookie
// It's a bit similar to the anonymous class.
VaR expires = function ()... {// cookie expiration time is within 48 hours
VaR mydate = new date ();
Mydate. settime (mydate. gettime + 48*60x60*1000 );
Return mydate. togmtstring ();
}
If (sname. length! = 0 & svalue. length! = 0 )...{
Document. Cookie = sname + "=" + svalue + "; expires =" + expires;
} Else ...{
Alert ("You are blank! ");
}
}
/**//*********************************** ******
Delete cookie
**************************************** ***/
Function delcookie (sname, svalue)... {// delete a specified cookie key-Value Pair
Document. Cookie = sname + "=" + escape (svalue) + "; expires = Fri, 31 Dec 1999 23:59:59 GMT ;";
}
/**//*********************************** ******
Delete cookie
**************************************** ***/
Function delcookie (sname )...{
VaR svalue = getcookie (sname );
If (svalue! = NULL )...{
Document. Cookie = sname + "=" + escape (svalue) + "; expires = Fri, 31 Dec 1999 23:59:59 GMT ;";
}
}
/**//*********************************** ******
Clear cookie
**************************************** ***/
Function clearcookie ()...{
VaR value = Document. Cookie;
VaR arr1 = value. Split (";");
For (I = arr1.length-1; I> = 0; I --)...{
If (value. Length = 0 )...{
Break;
}
Sname = arr1 [I]. Split ("=") [0];
Svalue = arr1 [I]. Split ("=") [1]
Document. Cookie = sname + "=" + escape (svalue) + "; expires = Fri, 31 Dec 1999 23:59:59 GMT ;";
}
}

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.