The first method: the cookie operation class, the code is encapsulated, and there are also usage methods below, you can refer to it.
= function()
{
return (/^\s+/g,"").replace(/\s+$/g,"");
}
function JSCookie()
{
= function(key)
{
var cookie = ;
var cookieArray = (';');
var getvalue = "";
for(var i = 0;i<;i++)
{
if(cookieArray[i].Trim().substr(0,) == key)
{
getvalue = cookieArray[i].Trim().substr( + 1);
break;
}
}
return getvalue;
};
= function(cookiekey,childkey)
{
var child = (cookiekey);
var childs = ('&');
var getvalue = "";
for(var i = 0;i < ;i++)
{
if(childs[i].Trim().substr(0,) == childkey)
{
getvalue = childs[i].Trim().substr( + 1);
break;
}
}
return getvalue;
};
= function(key,value,expire,domain,path)
{
var cookie = "";
if(key != null && value != null)
cookie += key + "=" + value + ";";
if(expire != null)
cookie += "expires=" + () + ";";
if(domain != null)
cookie += "domain=" + domain + ";";
if(path != null)
cookie += "path=" + path + ";";
= cookie;
};
= function(key)
{
expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() - 1);
var cookie = " " + key + "=e;expires=" + expire_time + ";"
= cookie;
}
}
usage:
1. Set cookies
var cookie = new JSCookie();
//Normal 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,".","/");
//Set the cookies with subkeys, and the subkeys are k1, k2, k3 respectively.
cookie .SetCookie("key4","k1=1&k2=2&k3=3");
2. Read cookies
//Simple access
cookie .GetCookie("key1");
cookie .GetCookie("key2");
cookie .GetCookie("key3");
cookie .GetCookie("key4");
//Get the value of key4's subkey k1
cookie .GetChild("key4","k1");
3. Delete
cookie .Expire("key1");
cookie .Expire("key2");
cookie .Expire("key3");
cookie .Expire("key4");
The second method: cookie operation function, I also use this. You can choose according to your needs.
function setCookie(name, value) //cookies set JS
{
var argv = ;
var argc = ;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
(() + (expires*1000*3600*24));
}
= name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +()));
}
function getCookie(Name) //cookies read JS
{
var search = Name + "="
if( > 0)
{
offset = (search)
if(offset != -1)
{
offset +=
end = (";", offset)
if(end == -1) end =
return unescape((offset, end))
}
else return ""
}
}
How to use:
if(getCookie("yxjok")!="ok"){
//Judge whether the value of yxjok in the cookie is ok, and the following advertisement is displayed.
('<div ><a href="https://" onclick="Closeyxj()" target="_blank"><img src="https:///images/"
/></a></div>');
}
function Closeyxj(){
//Close the reality of advertising. And the record of using cookies has been displayed. The main function here is that if it is not displayed for a period of time after being turned off, the default is 24 hours.
$("jb51_yxj").='none';
setCookie("yxjok","ok",10);
}
function setADCookie(name, value) // It mainly changes the expiration time of cookies to several minutes.
{
var argv = ;
var argc = ;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
(() + (expires*1000*300));
}
= name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +()));
}
Copy the codeThe code is as follows:
= function()
{
return (/^\s+/g,"").replace(/\s+$/g,"");
}
function JSCookie()
{
= function(key)
{
var cookie = ;
var cookieArray = (';');
var getvalue = "";
for(var i = 0;i<;i++)
{
if(cookieArray[i].Trim().substr(0,) == key)
{
getvalue = cookieArray[i].Trim().substr( + 1);
break;
}
}
return getvalue;
};
= function(cookiekey,childkey)
{
var child = (cookiekey);
var childs = ('&');
var getvalue = "";
for(var i = 0;i < ;i++)
{
if(childs[i].Trim().substr(0,) == childkey)
{
getvalue = childs[i].Trim().substr( + 1);
break;
}
}
return getvalue;
};
= function(key,value,expire,domain,path)
{
var cookie = "";
if(key != null && value != null)
cookie += key + "=" + value + ";";
if(expire != null)
cookie += "expires=" + () + ";";
if(domain != null)
cookie += "domain=" + domain + ";";
if(path != null)
cookie += "path=" + path + ";";
= cookie;
};
= function(key)
{
expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() - 1);
var cookie = " " + key + "=e;expires=" + expire_time + ";"
= cookie;
}
}
usage:
1. Set cookies
var cookie = new JSCookie();
//Normal 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,".","/");
//Set the cookies with subkeys, and the subkeys are k1, k2, k3 respectively.
cookie .SetCookie("key4","k1=1&k2=2&k3=3");
2. Read cookies
//Simple access
cookie .GetCookie("key1");
cookie .GetCookie("key2");
cookie .GetCookie("key3");
cookie .GetCookie("key4");
//Get the value of key4's subkey k1
cookie .GetChild("key4","k1");
3. Delete
cookie .Expire("key1");
cookie .Expire("key2");
cookie .Expire("key3");
cookie .Expire("key4");
The second method: cookie operation function, I also use this. You can choose according to your needs.
Copy the codeThe code is as follows:
function setCookie(name, value) //cookies set JS
{
var argv = ;
var argc = ;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
(() + (expires*1000*3600*24));
}
= name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +()));
}
function getCookie(Name) //cookies read JS
{
var search = Name + "="
if( > 0)
{
offset = (search)
if(offset != -1)
{
offset +=
end = (";", offset)
if(end == -1) end =
return unescape((offset, end))
}
else return ""
}
}
How to use:
Copy the codeThe code is as follows:
if(getCookie("yxjok")!="ok"){
//Judge whether the value of yxjok in the cookie is ok, and the following advertisement is displayed.
('<div ><a href="https://" onclick="Closeyxj()" target="_blank"><img src="https:///images/"
/></a></div>');
}
function Closeyxj(){
//Close the reality of advertising. And the record of using cookies has been displayed. The main function here is that if it is not displayed for a period of time after being turned off, the default is 24 hours.
$("jb51_yxj").='none';
setCookie("yxjok","ok",10);
}
function setADCookie(name, value) // It mainly changes the expiration time of cookies to several minutes.
{
var argv = ;
var argc = ;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
(() + (expires*1000*300));
}
= name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +()));
}