first, from the writing cookie.。
var the_date = new Date ("December 31, 2020");
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 GMT format; The editor: GMT, Greenwich Mean Time, is now also called UTC, the global standard Time.
The third sentence is to write the cookie content to the client.
Where the expires is used by the system, indicating the expiration date of the cookie (also can be omitted), expires unreadable.
Escape is the encoding of the cookie value, which is set up to handle Chinese, white space, and so on.
Second, the cookie is relatively simple.
function Getcss ()
{
var cookiestr = Document.cookie; The cookie string is not readable, so expires will not appear in COOKIESTR because expires is unreadable.
if (cookiestr = "")
{
return "Main1"; The cookie string is not taken and the default value is returned
}
var cookievalue = Cookiestr.split (";"); Separate cookies from each other, with an array, multiple cookies separated by semicolons, but we used only one cookie, and the value is separated from the expires by a semicolon plus a space.
Copy Code code as follows:
var varName = "Userdefinecss";
var startpos =-1;
var endpos =-1;
for (var i=0; i<cookievalue.length; i++)
{
startpos = Cookievalue[i].indexof (varName);
if (startpos!= 0)
{
Continue The current cookie is not a cookie named VarName, and the next cookie is judged
}
Startpos + = varname.length + 1; The current cookie is a cookie with the name VarName, and because there is an equal sign, +1
Endpos = Cookievalue[i].length;
var css = unescape (cookievalue[i].substring (Startpos, endpos));
return CSS;
}
return "Main1";
}
Because escape is used to write cookies, the cookie value is returned using unescape for decoding.
proper use of cookies ' properties
First look at the structure of cookies.
The cookie structure we are talking about is not its storage structure, but its performance structure, mainly by studying its performance structure to realize the operation of JS (JavaScript) to cookies.
Cookies have a simpler performance structure, each cookie consists of a cookie name and cookie value, an equal sign to indicate the relationship, and separate cookies separated by a semicolon plus a space. As previously said expires, path, domain are not readable, so in the performance structure is not reflected.
cookiename1=cookievalue1; Cookiename2=cookievalue2[...; Cookienamen=cookievaluen]
By separating the semicolon plus the space symbol, you can get each cookie, and by separating the equals sign, you can draw the name and value of each cookie.
The subkeys of a cookie are only shown on the Cookievalue, and the structure of a subkey is: the subkey name = subkey value, and the multiple subkeys are connected by &. Like what:
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 a string in the cookie structure: aspsessionidqstdratq=24 bit character
About this, view: http://www.aspxuexi.com/aspbasic/cookie/2006-6-10/Session_Cookie.htm
Cookies of the same name, different domain or different path, are different cookies;
Cookies with the same name, same domain and same path, different expires, belong to the same cookie.
The cookie has a path--path that indicates which files under the path have permission to read the cookie.
Path should end with "/", cookie with same name, different path, different cookie
Document.cookie = "n1=1; path=/path/";
Document.cookie = "n1=2; Path=/path ";
Document.cookie = "n1=3; path=path/";
As above code, the first two sentences use absolute path, that is, relative to the site root directory of the Web page, the third sentence uses relative path, relative to the current directory.
The first and second sentences are different at the end, although they express the same permissions, but because the path string is different, will create two of the same name cookies, easy to create confusion, we recommend not to use the second sentence this format, because the system defaults to "/" end.
So if the above is three cookies, it will not cover each other.
The Path property value is case-sensitive and should match the input to the address bar in the browser
Document.cookie = "n1=1; path=/path/";
Document.cookie = "n1=2; path=/path/";
This is two different cookies because the Path property value is not case-sensitive, if we enter path in the address bar, we read the first N1, and if we enter path, we read the second N1
Path not readable
As with expires, path is only writable and unreadable.
Path cannot be changed
Unlike expires, if we try to change path, we actually write a different cookie instead of changing the path value.
Path Permissions are inherited
If you specify that the/test/directory has permission to read a cookie, the directory/test/t/under/test/also has permission to read the cookie.
Cookies have expiration date--expires, if there is no expiration period, even if the computer is restarted, the cookie will not be lost, if the expires value is not specified, then the cookie is invalidated when the browser is closed.
When using expires in JS (JavaScript), it should be written with a cookie, such as:
Document.cookie = "clr=red; expires= "+ expiresdate;
The following wording is not correct:
Document.cookie = "clr=red";
Document.cookie = "expires=" + expiresdate;
This will create two cookies, the name of the second cookie is expires, and two cookies do not specify the expiration date.
Expires not readable
That's why we use response in ASP. Write request.cookies ("CNAME"). Expires error, also in JS (JavaScript) using Document.cookie will not show expires.
Expires value should be in GMT format
var the_date = new Date ("December 31, 2020");
var expiresdate = the_date.togmtstring (); Convert to GMT format.