The examples in this article describe JavaScript's basic operations for cookies. Share to everyone for your reference, specific as follows:
JS is considered a notorious affiliate programming language by C # and Java developers, for example, the operation of cookies. JS does not have a similar set of C # ready-made solution, but only by yourself to complete. Below I will learn the object-oriented thinking of the cookie processing of the learning notes to organize, readers.
Common operation analysis for cookies:
(1) Setting up cookies includes adding and modifying functions, in fact, if the original cookie name already exists, then adding this cookie is equivalent to modifying the cookie. There may also be options for setting cookies, which refer to the declaration cycle of the cookie, access path, and access domain and security, and so on. In order for the cookie to be able to store Chinese, the stored value is also encoded in the method.
(2) The value of a cookie that receives the cookie name called a parameter and returns the value of the cookie. Since the value has been encoded when it is stored, the value should be automatically decoded and then returned (here you can actually set what to return, not just "take a value").
(3) Delete a cookie, delete a cookie simply set the expiration of a cookie to the past one time, it receives a cookie name as a parameter, thereby removing this cookie (my implementation also sets the cookie name to NULL, This is due to the possibility that more than one cookie will be set in the future, the name of the conflict occurs.
(4) Others (here let the reader consider other operations themselves, do not repeat.) )
Well, you must have guessed what I was going to say, right, the code is cheap. Look at the codes:
/* operation of cookies///create var Cookie = new Object (); Set (Modify) properties and methods Cookie.setcookie = function (sname, svalue, Oexpires, spath, Sdomain, bsecure) {var SCookie = sname + "=" + Escape (svalue); Name and value if (oexpires) {SCookie + = "; Expires= "+ oexpires.togmtstring (); Expiration Time} if (spath) {SCookie + = "; Path= "+ spath; Access path} if (Sdomain) {SCookie + = "; domain= "+ sdomain; Access path} if (bsecure) {SCookie + = "; True ";
Security} document.cookie = SCookie; }//Get Cookie.getcookie = function (sname) {var cookiearray = Document.cookie.split (";");//Get segmented name value to var Tempcookie =
New Object (); for (var i = 0; i < cookiearray.length i++) {var Temparr = cookiearray[i].split ("=");//separate name and value if (temparr[0
= = = sname) {//If the specified cookie returns its value return Unescape (Temparr[1]);
} return "There ' s no such a cookie name!"; }//Delete Cookie.deletecookie = function (sname, spath, Sdomain) {var SCookie = sname + "=; Expires= "+ (new Date (0)). togmtstrING (); The setting name is empty, the expiration time is 0, or you can set the expiration time to be negative (var SCookie = sname + "=; Expires=-1 "; if (spath) {SCookie + = ";
Path= "+ spath; } if (Sdomain) {SCookie + = ";
domain= "+ sdomain;
} document.cookie = SCookie;
function test () {Cookie.setcookie ("test", "CookieTest");
Alert (Cookie.getcookie ("Test")); Alert (Cookie.getcookie ("test2"));
// ???
Cookie.deletecookie ("test");
Alert (Cookie.getcookie ("Test"));
}
Add: JavaScript Action Cookie class
String.prototype.Trim = function () {return this.replace (/^\s+/g, ""). Replace (/\s+$/g, "");} function Jscookie () {thi
S.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 &&Amp
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:
One, set cookies
var cookie = new Jscookie ();
Normal settings
cookie. Setcookie ("Key1", "Val1");
The expiration time is one year
var expire_time = new Date ();
Expire_time.setfullyear (Expire_time.getfullyear () + 1);
Cookies. Setcookie ("Key2", "Val2", expire_time);
Sets the domain and path with the expiration Time
cookie. Setcookie ("Key3", "Val3", Expire_time, ". cnblogs.com", "/");
Set the cookie for the Ribbon key, which is the K1,K2,K3 cookie, respectively
. 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 cookie for the Key4
. Getchild ("Key4", "K1");
Third, delete
Cookies. Expire ("Key1");
Cookies. Expire ("Key2");
Cookies. Expire ("Key3");
Cookies. Expire ("Key4");
I hope this article will help you with JavaScript programming.