The example of this article describes the JS encapsulation cookie operation function. Share to everyone for your reference, specific as follows:
/* Set cookie*/
function Setcookie (name, value, Iday)
{
var odate=new Date ();
Odate.setdate (Odate.getdate () +iday);
document.cookie=name+ ' = ' +value+ '; expires= ' +odate;
};
/* Usage: Setcookie (' User ', ' Simon ', one); *///
* Get cookie*/
function GetCookie (name)
{
var arr= Document.cookie.split ('; '); Multiple cookie values are in; Separate, split the cookie and assign it to the array for
(var i=0;i<arr[i].length;i++)//calendar array
{
var arr2=arr[i].split (' = ') ; The original cut of the array is: User=simon, and then split (' = ') divided into: User Simon can be arr2[0] arr2[1] To obtain the user and Simon
if (arr2[0]==name)// If the property name of the array is equal to the incoming name
{return
arr2[1];//
returns null
}
if not found; /* Usage: GetCookie (' user ') *////
* Delete cookie*/
function Removecookie (name)
{
Setcookie (name, 1,-1); -1 is to tell the system has expired, the system will immediately delete the cookie
};
/* Use method: Removecookie (' user ') * *
I hope this article will help you with JavaScript programming.