Summary of the implementation code of javascript cookie operations

Source: Internet
Author: User
Tags set cookie

Method 1: cookie operation class, code encapsulated. The following methods are also available for your reference.
Copy codeThe Code is as follows:
String. prototype. Trim = function ()
{
Return this. replace (/^ \ s +/g, ""). replace (/\ s + $/g ,"");
}
Function JSCookie ()
{
This. GetCookie = function (key)
{
Var cookie = document. cookie;
Var cookieArray = cookie. split (';');
Var getvalue = "";
For (var I = 0; I <cookieArray. length; I ++)
{
If (cookieArray [I]. Trim (). substr (0, key. length) = key)
{
Getvalue = cookieArray [I]. Trim (). substr (key. length + 1 );
Break;
}
}
Return getvalue;
};
This. GetChild = function (cookiekey, childkey)
{
Var child = this. GetCookie (cookiekey );
Var childs = child. split ('&');
Var getvalue = "";
For (var I = 0; I <childs. length; I ++)
{
If (childs [I]. Trim (). substr (0, childkey. length) = childkey)
{
Getvalue = childs [I]. Trim (). substr (childkey. length + 1 );
Break;
}
}
Return getvalue;
};
This. SetCookie = function (key, value, expire, domain, path)
{
Var cookie = "";
If (key! = Null & value! = Null)
Cookie + = key + "=" + value + ";";
If (expire! = Null)
Cookie + = "expires =" + expire. toGMTString () + ";";
If (domain! = Null)
Cookie + = "domain =" + domain + ";";
If (path! = Null)
Cookie + = "path =" + path + ";";
Document. cookie = cookie;
};
This. Expire = function (key)
{
Expire_time = new Date ();
Expire_time.setFullYear (expire_time.getFullYear ()-1 );
Var cookie = "" + key + "= e; expires =" + expire_time + ";"
Document. cookie = cookie;
}
}

Usage:
1. Set cookie
Var cookie = new JSCookie ();
// Normal settings
Cookie. SetCookie ("key1", "val1 ");
// The expiration time is one year.
Var expire_time = new Date ();
Expire_time.setFullYear (expire_time.getFullYear () + 1 );
Cookie. SetCookie ("key2", "val2", expire_time );
// Set the domain and path with the expiration time
Cookie. SetCookie ("key3", "val3", expire_time, ".cnblogs.com ","/");
// Set the cookie of the BIND key. The subkeys are k1, k2, and k3.
Cookie. SetCookie ("key4", "k1 = 1 & k2 = 2 & k3 = 3 ");
Ii. Reading cookies
// Simple retrieval
Cookie. GetCookie ("key1 ");
Cookie. GetCookie ("key2 ");
Cookie. GetCookie ("key3 ");
Cookie. GetCookie ("key4 ");
// Obtain the key 1 Value of key4
Cookie. GetChild ("key4", "k1 ");
Iii. Delete
Cookie. Expire ("key1 ");
Cookie. Expire ("key2 ");
Cookie. Expire ("key3 ");
Cookie. Expire ("key4 ");
Method 2: cookie operation function, which is also used by the fool house. You can select as needed.
Copy codeThe Code is as follows:
Function setCookie (name, value) // set JS for cookies
{
Var argv = setCookie. arguments;
Var argc = setCookie. arguments. length;
Var expires = (argc> 2 )? Argv [2]: null;
If (expires! = Null)
{
Var LargeExpDate = new Date ();
LargeExpDate. setTime (LargeExpDate. getTime () + (expires * 1000*3600*24 ));
}
Document. cookie = name + "=" + escape (value) + (expires = null )? "": ("; Expires =" + LargeExpDate. toGMTString ()));
}

Function getCookie (Name) // cookies read JS
{
Var search = Name + "="
If (document. cookie. length> 0)
{
Offset = document. cookie. indexOf (search)
If (offset! =-1)
{
Offset + = search. length
End = document. cookie. indexOf (";", offset)
If (end =-1) end = document. cookie. length
Return unescape (document. cookie. substring (offset, end ))
}
Else return ""
}
}

Usage:
Copy codeThe Code is as follows:
If (getCookie ("yxjok ")! = "OK "){
// Determine whether the yxjok value in the cookie is OK. If not, the following advertisement is displayed.
Document. write ('<div id = "jb51_yxj"> <a href = "http://www.jb51.net" onclick = "Closeyxj ()" target = "_ blank"> /> </A> </div> ');
}
Function Closeyxj (){
// Close the reality of advertising. The cookie record has already been used. The function here is mainly to disable the display for a period of time. The default value is 24 hours.
$ ("Jb51_yxj"). style. display = 'none ';
SetCookie ("yxjok", "OK", 10 );
}

Function setADCookie (name, value) // mainly modifies the cookie expiration time, which is several minutes.
{
Var argv = setADCookie. arguments;
Var argc = setADCookie. arguments. length;
Var expires = (argc> 2 )? Argv [2]: null;
If (expires! = Null)
{
Var LargeExpDate = new Date ();
LargeExpDate. setTime (LargeExpDate. getTime () + (expires * 1000*300 ));
}
Document. cookie = name + "=" + escape (value) + (expires = null )? "": ("; Expires =" + LargeExpDate. toGMTString ()));
}

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.