Implementation code Summary of JavaScript cookie operation class _javascript tips for using a method

Source: Internet
Author: User
Tags setcookie
The first method: Cookie operation class, code encapsulation, the following also have the use of methods, we can refer to the following.
Copy Code code 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)
Cookies = + "expires=" + expire.togmtstring () + ";";
if (domain!= null)
Cookies = + "domain=" + domain + ";";
if (path!= null)
Cookies = + "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:
One, set cookies
var cookie = new Jscookie ();
Normal settings
Cookies. Setcookie ("Key1", "val1");
Expiration time is one year
var expire_time = new Date ();
Expire_time.setfullyear (Expire_time.getfullyear () + 1);
Cookies. Setcookie ("Key2", "Val2", expire_time);
Set the domain and path with expiration time
Cookies. Setcookie ("Key3", "Val3", Expire_time, ". cnblogs.com", "/");
Set the cookie for the Ribbon key, which is K1,K2,K3, respectively.
Cookies. Setcookie ("Key4", "k1=1&k2=2&k3=3");
second, read cookies
Easy to get
Cookies. GetCookie ("Key1");
Cookies. GetCookie ("Key2");
Cookies. GetCookie ("Key3");
Cookies. GetCookie ("Key4");
Gets the subkey K1 value of the Key4
Cookies. Getchild ("Key4", "K1");
Third, delete
Cookies. Expire ("Key1");
Cookies. Expire ("Key2");
Cookies. Expire ("Key3");
Cookies. Expire ("Key4");
The second method: The cookie operator function, which is also used by the cloud-dwelling community. You can choose according to your needs.
Copy Code code 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 (offset, end) (document.cookie.substring)
}
else return ""
}
}

How to use:
Copy Code code as follows:

if (GetCookie ("Yxjok")!= "OK") {
Determine if the Yxjok value in the cookie is not OK, not the advertisement below.
document.write (' <div id= "JB51_YXJ" ><a href= "http://www.jb51.net" onclick= "Closeyxj ()" target= "_blank" > /></a></div> ');
}
function Closeyxj () {
Turn off the reality of advertising. And the cookies record has been shown, here the function is mainly closed after a period of time does not show the default is 24 hours.
$ ("Jb51_yxj"). style.display= ' None ';
Setcookie ("Yxjok", "OK", 10);
}

function Setadcookie (name, value)//mainly modifies the expiration time of cookies for a few 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 ()));
}

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.