Home > Web Front-end > JS Tutorial > body text

Summary of the implementation code of the javascript cookie operation class with usage methods_javascript skills

WBOY
Release: 2016-05-16 18:25:40
Original
1133 people have browsed it
The first method: cookie operation class, the code is encapsulated, there are also usage methods below, you can refer to it.
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{
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 children = child.split('&');
var getvalue = "";
for(var i = 0;i < childs.length;i )
{
if(childs[i].Trim().substr(0,childkey.length) == childkey)
{
getvalue = children[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 cookies
var cookie = new JSCookie();
//Common settings
cookie .SetCookie("key1","val1");
//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 expiration time
cookie .SetCookie("key3","val3",expire_time,".cnblogs.com","/");
//Set a cookie with subkeys, the subkeys are k1, k2 ,k3
cookie .SetCookie("key4","k1=1&k2=2&k3=3");
2. Read cookie
//Easy to get
cookie. GetCookie("key1");
cookie .GetCookie("key2");
cookie .GetCookie("key3");
cookie .GetCookie("key4");
//Get key4 The subkey k1 value
cookie .GetChild("key4","k1");
3. Delete
cookie .Expire("key1");
cookie . Expire("key2");
cookie .Expire("key3");
cookie .Expire("key4");
Second method: cookie operation function, Script Home also Use this. Everyone can choose according to their needs.
Copy code The code is as follows:

function setCookie(name, value) // Cookies setting 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"){
//Determine whether the value of yxjok in the cookie is ok, if not, display the following advertisement.
document.write('');
}
function Closeyxj() {
// Turn off the reality of advertising. And the cookie record has been displayed. The main function here is not to display for a period of time after closing. The default is 24 hours.
$("jb51_yxj").style.display='none';
setCookie("yxjok","ok",10);
}

function setADCookie(name, value ) //Mainly modify the expiration time of cookies to 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()));
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template