This article mainly introduces document in Javascript. cookie is used to remember the password and save the password. For more information, see the next article about document in Javascript. use of cookies. Use cookies to remember and save passwords. For more information, see
Set cookie
Each cookie is a name/value pair. You can assign the following string to document. cookie:
document.cookie="userId=828";
To store multiple name/value pairs at a time, use semicolons (;) to separate them. For example:
document.cookie="userId=828; userName=hulk";
The names or values of cookies cannot contain semicolons (;), commas (,), equal signs (=), and spaces.
It is easy to do this in the cookie name, but the value to be saved is uncertain. How can we store these values?
The method is encoded using the escape () function. It can represent some special symbols in hexadecimal notation. For example, spaces are encoded as "20%", which can be stored in cookie values, in addition, this solution can avoid Chinese garbled characters. For example:
document.cookie="str="+escape("I love ajax");
Equivalent to: document. cookie = "str = I % 20 love % 20 ajax ";
After escape () encoding is used, you must use unescape () to decode the extracted value to obtain the original cookie value, which has been described earlier. Although document. cookie looks like an attribute, different values can be assigned. However, it is different from a general attribute. changing its value assignment does not mean losing the original value. For example, you can execute the following two statements consecutively:
document.cookie="userId=828";document.cookie="userName=hulk";
In this case, the browser will maintain two cookies, namely userId and userName. Therefore, assigning a value to document. cookie is more like executing a statement like this:
document.addCookie("userId=828");document.addCookie("userName=hulk");
In fact, the browser sets the cookie in this way. To change the value of a cookie, you only need to assign a value again. For example:
document.cookie="userId=929";
In this way, set the cookie value named userId to 929.
Obtain the cookie value.
The following describes how to obtain the cookie value. The cookie value can be directly obtained by document. cookie:
var strCookie=document.cookie;
This will obtain a string consisting of multiple name/value pairs separated by semicolons. These name/value pairs include all cookies under the domain name. For example: