Usage:
One, set cookies
Copy Code code as follows:
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
Copy Code code as follows:
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
Copy Code code as follows:
Cookies. Expire ("Key1");
Cookies. Expire ("Key2");
Cookies. Expire ("Key3");
Cookies. Expire ("Key4");
Example:
Copy Code code as follows:
<script type= "Text/javascript" >
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;
}
}
</script>
read cookie values in a regular way
Because the ASP's cookies are URL-coded, you should unescape them before you read them.
Here's how I use the regular method to fetch cookies, including single and multivalued:
Copy Code code as follows:
/**************************************************
Parameter description:
Smainname Cookie Name
Ssubname cookie subkey name, left blank to represent a single-value cookie
**************************************************/
function GetCookie (smainname, Ssubname)
{
var re = new RegExp (ssubname smainname + "= (?:.*?&) *" + ssubname + "= ([^&;$]*)": Smainname + "= ([^;$]*)"), "I" );
Return Re.test (unescape (document.cookie))? Regexp["$"]: "";
}