The examples in this article describe the methods that JavaScript implements to set up, get, and delete cookies. Share to everyone for your reference. The implementation method is as follows:
* * Set the corresponding value of the key * Value:cookie of the cookie * * * * * * * * Name:cookie * Expires:cookie The valid time * path: Specifies the path of the accessible cookie * DOMA
In: Specify host name for accessible cookies * Secure: Security/function Setcookie (name,value,expires,path,domain,secure) {//cookie key value pair
var str = name + "=" + escape (value);
Set the lifetime of the cookie, in hours if (expires > 0) {var date = new Date ();
var ms = Expires * 3600 * 1000;
Date.settime (Date.gettime () + ms); str + = ";
Expires= "+ date.togmtstring (); } if (path) {str + =;
Path= "+ path; } if (domain) {str =;
domain= "+ domain; } if (secure) {str =;
Secure ";
} document.cookie = str;
* * * Get the key/function GetCookie (cookie_name) {var value = null for cookie * *COOKIE_NAME:COOKIE;
var allcookies = Document.cookie;
var cookie_pos = Allcookies.indexof (cookie_name);
If an index is found, it means that the cookie exists,//otherwise, it does not exist.
if (Cookie_pos!=-1) {//Place the Cookie_pos at the beginning of the value, just add 1 to the value.Cookie_pos + = cookie_name.length + 1;
var cookie_end = Allcookies.indexof (";", Cookie_pos);
if (cookie_end = = 1) {cookie_end = Allcookies.length;
} value = Unescape (allcookies.substring (Cookie_pos, cookie_end));
return value;
* * * Delete cookie * *cookie_name:cookie key/function Delcookie (cookie_name) {var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var value = GetCookie (Cookie_name);
if (value) {document.cookie= cookie_name + "=" + Value + "; expires=" + exp.togmtstring ();
}
}
The
wants this article to help you with your JavaScript programming.