In JavaScript, cookies are complicated. In ASP, we only need to know the cookie name and cookie value. In JS, we are faced with cookie strings, write this string to the client and parse it yourself.
1. Starting from cookie writing.
Var the_date = new Date ("December 31,202 0 ");
Var expiresDate = the_date.toGMTString ();
Document. cookie = "userDefineCSS =" + escape (title) + "; expires =" + expiresDate;
The first sentence is the date object;
The second sentence converts the date format to the GMT format. Editor: GMT is Greenwich Mean Time, now also known as UTC is the global standard time.
The third sentence is to write the cookie content to the client.
Expires is used by the system to indicate the cookie expiration date (which can be omitted), and expires cannot be read.
Escape is used to encode the cookie value, which is set up to process Chinese characters and spaces.
2. cookie retrieval is relatively simple.
Function GetCSS ()
{
Var cookieStr = document. cookie; // obtain the cookie string. Because expires is not readable, expires will not appear in cookieStr.
If (cookieStr = "")
{
Return "main1"; // if no cookie string is obtained, the default value is returned.
}
Var cookieValue = cookieStr. split (";"); // separate each cookie and coexist as an array. Separate multiple cookies with semicolons (;). However, we only use one cookie, its values are separated by semicolons and spaces.
The Code is as follows:
Var varName = "userDefineCSS ";
Var startPos =-1;
Var endPos =-1;
For (var I = 0; I {
StartPos = cookieValue [I]. indexOf (varName );
If (startPos! = 0)
{
Continue; // The current cookie is not a cookie named varName. determine the next cookie.
}
StartPos + = varName. length + 1; // The current cookie is the cookie named varName. Because of the equal sign, + 1
EndPos = cookieValue [I]. length;
Var css = unescape (cookieValue [I]. substring (startPos, endPos ));
Return css;
}
Return "main1 ";
}
Because escape is used for writing cookies, unescape is used for decoding when the cookie value is returned.
Correct use of cookies
First, let's look at the structure of cookies.
The cookie structure we mentioned here is not its storage structure, but its presentation structure. It mainly studies its presentation structure to implement JS (JavaScript) operations on cookies.
The cookie presentation structure is relatively simple. Each cookie consists of a cookie name and a cookie value, and uses an equal sign to indicate the relationship between the two. Each cookie is separated by a semicolon and a space. As previously mentioned, expires, path, and domain are not readable, so they are not reflected in the presentation structure.
CookieName1 = cookieValue1; cookieName2 = cookieValue2 [...; cookieNamen = cookieValuen]
By separating semicolons and adding space characters, you can obtain each cookie, and then obtain the names and values of each cookie by separating the equal signs.
Cookie sub-keys are only represented on cookieValue. The structure of a sub-key is: Sub-key name = sub-key value, and multiple sub-keys are connected. For example:
CookieName1 = subkey name 1 = subkey value 1 & subkey name 2 = subkey value 2
If it is an ASP file, we will find that the cookie structure contains such a string: ASPSESSIONIDQSTDRATQ = 24 characters
About this, view: http://www.aspxuexi.com/aspbasic/cookie/2006-6-10/Session_Cookie.htm
Cookie with the same name. different domains or different paths belong to different cookies;
Cookie with the same name, the same domain and the same path, different expires, belong to the same cookie.
The cookie has the path -- path, which indicates which files have the permission to read the cookie.
Path should end with "/", cookie with the same name. Different paths belong to different cookies.
Document. cookie = "N1 = 1; path =/path /";
Document. cookie = "N1 = 2; path =/path ";
Document. cookie = "N1 = 3; path = path /";
In the preceding code, the first two statements use absolute paths, that is, relative paths relative to the webpage directory of the site root directory, and relative paths relative to the current directory.
The first and second sentences are different at the end. Although they have the same permissions, different paths may produce two cookies with the same name, which may lead to confusion, we recommend that you do not use the second sentence because the system end with "/" by default.
Therefore, if the preceding three cookies are used, they are not overwritten.
The path attribute values are case sensitive and should be consistent with the address bar input in the browser.
Document. cookie = "N1 = 1; path =/path /";
Document. cookie = "N1 = 2; path =/paTH /";
This is two different cookies, because the path attribute values are case-insensitive. If we enter path in the address bar, we will read the first N1. If we enter paTH, read the second N1
Path unreadable
Like expires, path is writable and unreadable.
Path cannot be changed
Unlike expires, if we try to change the path, we actually write another cookie instead of changing the path value.
The path permission is inherited.
If the/test/directory has the permission to read a cookie, the/test/t/directory under/test/also has the permission to read the cookie.
Cookie has an expiration date -- expires. If no expiration time expires is available, the cookie will not be lost even if the computer is restarted. If the expires value is not specified, the cookie will become invalid when the browser is closed.
When using expires in JavaScript, it should be written together with the cookie, for example:
Document. cookie = "clr = red; expires =" + expiresDate;
The following statement is incorrect:
Document. cookie = "clr = red ";
Document. cookie = "expires =" + expiresDate;
In this way, two cookies are generated. The second cookie is named expires, and no expiration date is specified for both cookies.
Expires unreadable
This is why we use response in ASP. write request. cookies ("cname "). an error occurs in expires, and document is also used in JS (JavaScript. the cookie does not display expires.
The expires value should be in GMT format.
Var the_date = new Date ("December 31,202 0 ");
Var expiresDate = the_date.toGMTString (); // convert to GMT format.