Summary of the implementation code of JavaScript cookie operations

Source: Internet
Author: User
Tags set cookie

Method 1: Cookie operations,CodeEncapsulation. The following methods are also available for your reference. Copy code The 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 code The Code is as follows: function setcookie (name, value) // cookies set JS
{
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 Code the code is as follows: if (getcookie (" yxjok ")! = "OK") {
// determines whether the yxjok value in the cookie is OK. If not, the following advertisement is displayed.
document. write ('

');
}< br> function closeyxj () {
// the reality of closing an advertisement. 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) // modifies the cookie expiration time, which is several minutes.
{< br> var argv = setadcookie. arguments;
var argc = setadcookie. Arguments. length;
var expires = (argc> 2 )? Argv [2]: NULL;
If (expires! = NULL)
{< br> var largeexpdate = new date ();
largeexpdate. settime (largeexpdate. gettime () + (expires * 1000*300);
}< br> 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.