1. Start by writing cookies .
var the_date = new Date("December 31, 2020");
var expiresDate = the_date.toGMTString();
document.cookie = "userDefineCSS=" escape(title) "; expires=" expiresDate ;
The first sentence is a date object;
The second sentence converts the date format into GMT format; Editor: GMT is Greenwich Mean Time, now also called UTC, which is Global Standard Time.
The third sentence is to write the cookie content to the client.
Among them expires is used by the system to indicate the expiration date of the cookie (can also be omitted), expires is unreadable.
Escape is to encode the cookie value, which is set up to handle Chinese, spaces, etc.
2. Retrieving cookies is relatively simple.
function GetCSS()
{
var cookieStr = document.cookie; //Get the cookie string. Since expires is unreadable, expires will not appear in cookieStr.
if (cookieStr == "")
{
return "main1"; //No cookie string was obtained, return the default value
}
var cookieValue = cookieStr.split(" ; "); //Separate each cookie and store it together as an array. Multiple cookies are separated by semicolons and spaces. However, we only used one cookie before, and its value and expires are also separated by semicolons.
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. Since there is an equal sign, 1
endPos = cookieValue[i].length;
var css = unescape(cookieValue[i].substring(startPos , endPos));
return css;
}
return "main1";
}
Since escape is used when writing cookies, unescape is used for decoding when returning cookie values.
Use the attributes of cookies correctly
First look at the structure of cookies
The cookie structure we are talking about here is not its storage structure, but its presentation structure, mainly by studying its presentation structure To implement JS (JavaScript) operations on cookies.
The performance structure of cookies is relatively simple. Each cookie consists of a cookie name and a cookie value. An equal sign is used to indicate the relationship between the two. Each cookie is separated by a semicolon and a space. As mentioned before, expires, path, and domain are not readable, so they are not reflected in the presentation structure.
cookieName1=cookieValue1; cookieName2=cookieValue2[...; cookieNamen=cookieValuen]
By separating semicolon and space symbol, you can get each cookie, and then by separating equal sign, you can get the name of each cookie. and value.
The subkey of cookie is only displayed on cookieValue. The structure of a subkey is: subkey name = subkey value, and multiple subkeys are connected with &. 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 there is such a string in the cookie structure: ASPSESSIONIDQSTDRATQ=24 characters
About this, check: http://www.aspxuexi.com/aspbasic/cookie/2006-6-10/Session_Cookie.htm
Cookies with the same name, different domains or different paths are different cookies. ;
Cookies with the same name, the same domain and the same path, but different expires belong to the same cookie.
Cookie has a path - path, which indicates which files in the path have permission to read the cookie.
Path should end with "/". Cookies with the same name and different paths belong to different cookies
document.cookie = "N1=1; path=/path/";
document.cookie = "N1= 2; path=/path";
document.cookie = "N1=3; path=path/";
The above code, the first two sentences use absolute paths, that is, relative to the site root directory For the web page directory, the third sentence uses a relative path, which is relative to the current directory.
The first and second sentences have different endings. Although they express the same permissions, due to different path strings, two cookies with the same name will be formed, which can easily cause confusion. We recommend not to use the second sentence. format, because the system default also ends with "/".
So as above, there are three cookies, and they will not cover each other.
The path attribute value is case-sensitive and should be consistent with the input in the address bar in the browser
document.cookie = "N1=1; path=/path/";
document.cookie = " N1=2; path=/paTH/";
These are two different cookies because the path attribute values are case-sensitive. If we enter path in the address bar, then the first N1 will be read. If We input paTH, then read the second N1
Path is not readable
Like expires, path can only be written, not readable.
Path cannot be changed
Unlike expires, if we try to change the path, we actually write another cookie instead of changing the path value.
Path permissions are inherited
If you specify that the /test/ directory has permission to read a cookie, then the directory /test/t/ under /test/ also has permission to read the cookie.
Cookies have an expiration date - expires. If the expiration date has not passed, the cookie will not be lost even if the computer is restarted. If the expires value is not specified, the cookie will expire when the browser is closed.
When using expires in JS (JavaScript), it should be written at the same time as the cookie, such as:
document.cookie = "clr=red; expires=" expiresDate;
The following writing is incorrect:
document.cookie = "clr=red";
document.cookie = "expires=" expiresDate;
Writing like this will form two cookies. The name of the second cookie is expires, and neither cookie has Specify expiration date.
Expires is not readable
This is why, when we use response.Write request.cookies("cname").expires in ASP, an error will occur. Similarly, when using document.cookie in JS (JavaScript), it will not be displayed. expires.
The expires value should use the time in GMT format
var the_date = new Date("December 31, 2020");
var expiresDate = the_date.toGMTString(); //Convert to GMT format.