Introduction:
Cookie is a message that records the user who has access to a site. It is actually left on the user's hard disk, even if the user has left a site, cookie still exists on the user's hard disk. If the user returns that site again, the cookie will In the server that is returned to the site, it is convenient for the site of the manager statistics and the information that processes the visitor. (Note: cookies is introduced into Netscape 2.0 and MSIE 3.0, but MSIE 3.0 is not too perfect for cookies. You can use cookies to save and read information on other users' computers, but you can't save your cookies on your own computer. Therefore, it is difficult to test the test of cookie. So if you use MSIE 3.0, it is best to upgrade it to msie 4.0 or switch to Netscape, it seems that everyone now uses MSIE 6.0 or more, so there is nothing to worry.)
Deepen:
Since cookies involve a hard disk writing disk and read information to the user, it involves a confidential issue. If you need a lot of cookies, you should read the Cooikies written by Marc Slayton and re-examine the cookies. These articles will tell cookie's substance and scope of action and its intrinsic limitations. The most important limitations are: not everyone's browser welcomes cookies. Even the user's browser welcomes cookies, but users may also reject cookies access (most people still welcome), each domain name only assigns 20 cookies, so saving the use of them. Cookies must not be greater than 4KB, of course, 4,000 bytes of capacity is sufficient.
Setting up a basic cookie is easy. What you need to do is just generated a string in a cookie_name = value form, then set the Document.cookie property. Unique skills: There is no space, comma or semicolon in the cookie value. Fortunately, you don't have to worry about these issues, because there is a series of functions to help you encode and decode cookies attribute: escape () and unescape ().
Example: var the_cookie = escape (the_name);
Document.cookie = the_cookie;
Escape () replaces the space in the_name with% 20, and unscape () replaces it to space.
Read:
Use Document.cookie to read the cookies related to the web page. Example: var cookieuserinfo = "Userinfo = name: ZHOULEI / AGE: 25 / sex: man"; Document.cookie = CookieUserInfo; slashots / and colons are not absolute selection, you can use any character to make divided signs, as long as you pay attention The same limiter can be used when decoding the cookie. 1. A cookie reading, including several information
Web a var cookieuserinfo = "Userinfo = name: zhoulei / Age: 25 / sex: man"; Document.cookie = CookieUserInfo;
Web B
SetCookieArray (Document.cookie)
Function setCookieArray (cookievalue) {
Var cookiearray = new arrival ();
IF (cookievalue == '|| cookievalue == null) {Return NULL;
}
VAR SEPARATED_VALUES = CookieValue.split ("/");
For (Var loop = 0; loop Var Broken_info = Separated_Values [loop] .split (":"); Var the_property = broker_info [0]; Var the_value = broker_info [1]; CookieArray [The_Property] = the_value; } Return CookieArray; } 2. A number of reads, including several information Page A var cookieUserInfo = "userinfo = name: zhoulei / age: 25 / sex: man"; var cookieSystemInfo = "systeminfo = hostname: 192.168.0.1/os: windowsXP / database: oracle"; document.cookie = cookieUserInfo; document. Cookie = cookiesysteminfo; Web B SetCookieArray ("UserInfo"); // Read the single cookie value of the specified in the page cookie Function getCookieValue (Name) { Var RetValue; IF (Document.cookie == ') { Return NULL; } Var firstchar, Lasechar; Var cookieTemp = document.cookie; Firstchar = cookieTemp.Indexof (name); IF (firstchaar! = -1) { Firstchar = Name.length 1; LaSechar = cookieTemp.indexof (";", firstchar); IF (LASECHAR == -1) { LASECHAR = CookieTemp.Length; } RetValue = cookieTemp.substring (firstchar, lasecha); Return RetValue; } else { Return NULL; } } // Decompose multiple information contained in a single cookie to become related arrays Function setCookieArray (cookievalue) { Var cookiearray = new arrival (); IF (cookievalue == '|| cookievalue == null) { Return NULL; } VAR SEPARATED_VALUES = CookieValue.split ("/"); For (Var loop = 0; loop Var Broken_info = Separated_Values [loop] .split (":"); Var the_property = broker_info [0]; Var the_value = broker_info [1]; CookieArray [The_Property] = the_value; } Return CookieArray; } aging: Cookies often be automatically deleted when the user shuts its browser. If you want to set the cookie's failure time, you need a special format called GMT. example: MON, 27-APR-1998 00:00:00 GMT JavaScript has a date with a date called TogmTString to easily set the GMT. Example: var the_date = new date ("december 31, 2023"); var the_cookie_date = the_date.togmtstring (); once the cookie's failure period, its cookie's information settings should be: Document.cookie = "UserInfo = Name: ZHOULEI / AGE: 25 / sex: man; expires = THE_COOKIE_DATE "; Path and domain "path = /; domain = sina.com.cn;"