Javascript is justified by developers such as C # and JAVA as the notorious affiliated programming language, such as cookie operations. Js does not have a ready-made processing solution similar to C #, but it can only be done by yourself. Next, I will sort out the learning notes that I learned to process cookies with object-oriented thinking to serve readers.
Common cookie operations:
(1) setting a cookie includes the ADD and modify functions. In fact, if the original cookie name already exists, adding the cookie is equivalent to modifying the cookie. When setting a cookie, you may have some other options, such as the cookie statement cycle, access path, access domain, and security. To enable the cookie to store Chinese characters, the stored values must be encoded in this method.
(2) obtain the value of a cookie. The method receives the cookie name as a parameter and returns the value of the cookie. Because the encoding has been performed when the value is stored, the value should be automatically decoded and then returned (here you can set what to return, not just "get a value ).
(3) Delete a cookie. to delete a cookie, you only need to set the expiration time of a cookie to the past time. It receives the name of a cookie as a parameter, to delete this cookie (My implementation also sets the cookie name to null, Which is because the name may conflict when multiple cookies are to be set in the future ).
(4) Others (here, let the readers consider other operations and leave them blank .)
Okay, you must have guessed what I want to say again. Right, code is cheap. Check the code:
Code
/* Cookie operations */
// Create
Var Cookie = new Object ();
// Set (modify) attributes 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;
}
// Obtain
Cookie. getCookie = function (sName ){
Var cookieArray = document. cookie. split (";"); // obtain the split name value pair.
Var tempCookie = new Object ();
For (var I = 0; I <cookieArray. length; I ++ ){
Var tempArr = cookieArray [I]. split ("="); // separate names and values
If (tempArr [0] = sName) {// if the cookie is specified, its value is returned.
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 (); // set the name to null and the expiration time to 0. You can also set the expiration time to a negative value (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 "));
}
Finally, we recommend a blog article on cookie operations that is more useful. Compared with the cookie operation class of the following blogger, I can only serve as an introduction to beginners. Click for details:
Http://www.cnblogs.com/birdshover/archive/2008/07/15/1243007.html.