document.cookie="userId=828";
If you want to store multiple name/value pairs at one time, you can use semicolons and spaces (; ) to separate them, for example:
document.cookie="userId=828; userName=hulk";
Semicolons (;), commas (,), equal signs (=) and spaces cannot be used in cookie names or values. It's easy to do
in the name of the cookie, but the value to be saved is undefined. How to store these values? The method is to use the escape() function to encode
, which can express some special symbols in hexadecimal. For example, spaces will be encoded as "20%", which can be stored in
cookie value, and using this solution can also avoid the occurrence of Chinese garbled characters. For example:
document.cookie="str=" escape("I love ajax");
Equivalent to:
document.cookie="str=I love ajax";
When using escape( ) after encoding, you need to use unescape() to decode after taking out the value to get the original cookie value.
This has been introduced before.
Although document.cookie looks like a property and can be assigned different values. But it is different from general attributes. Changing the assignment of
does not mean losing the original value. For example, executing the following two statements continuously:
document.cookie="userId=828";
document.cookie="userName=hulk";
At this time, the browser will maintain two cookies, namely userId and userName, so assigning a value to document.cookie is more like executing
something like this Statement:
document.addCookie("userId=828");
document.addCookie("userName=hulk");
In fact, the browser sets cookies in this way. If you want To change the value of a cookie, just reassign the
value, for example:
document.cookie="userId=929";
This will set the cookie value named userId to 929.
Get the value of the cookie
The following describes how to get the value of the cookie. The value of the cookie can be obtained directly from document.cookie:
var strCookie=document.cookie;
This will get a string consisting of multiple name/value pairs separated by semicolons. These name/value pairs Includes all cookies
under this domain name. For example:
Figure 7.1 shows the output cookie value. It can be seen that you can only obtain all cookie values at once, but you cannot specify the cookie
name to obtain the specified value. This is the most troublesome part of processing cookie values. Users must analyze this string themselves to
get the specified cookie value. For example, to get the value of userId, you can do this:
This way you get the value of a single cookie
Using a similar method, you can get the value of one or more cookies. The main technique is still the related operations of strings and arrays.
Set an expiration date for cookies
Up to now, all cookies are single-session cookies, that is, these cookies will be lost after the browser is closed. In fact, these
are just It is stored in the memory without creating a corresponding hard disk file.
In actual development, cookies often need to be saved for a long time, such as saving the user's login status. This can be achieved using the following options:
document.cookie="userId=828; expires=GMT_String";
where GMT_String is a time string in GMT format, this The statement is to set the userId cookie to the expiration time represented by
GMT_String. After this time, the cookie will disappear and become inaccessible. For example: If you want to set the cookie
to expire after 10 days, you can do it like this:
Delete cookie
In order to delete a cookie, you can set its expiration time to time in the past, for example:
Specify the path where the cookie can be accessed
By default, if a cookie is created on a page, other pages in the directory where the page is located will also be accessible
The cookie. If there are subdirectories under this directory, you can also access it in the subdirectories. For example, a cookie created in
www.xxxx.com/html/a.html can be used by www.xxxx.com/html/b.html or
www.xxx.com /html/some/c.html is accessed, but cannot be accessed by www.xxxx.com/d.html.
In order to control the directory that cookies can access, you need to use the path parameter to set cookies. The syntax is as follows:
document.cookie="name=value; path=cookieDir";
where cookieDir represents the directory where cookies can be accessed. For example:
document.cookie="userId=320; path=/shop";
means that the current cookie can only be used in the shop directory.
If you want to make cookies available throughout the website, you can specify cookie_dir as the root directory, for example:
document.cookie="userId=320; path=/";
Specify The host name
that can access cookies is similar to the path. The host name refers to different hosts under the same domain. For example: www.google.com and gmail.google.com are
two different ones. hostname. By default, cookies created in one host cannot be accessed in another host.
But they can be controlled through the domain parameter. The syntax format is:
document.cookie ="name=value; domain=cookieDomain";
Take Google as an example. To achieve cross-host access, you can write:
document.cookie="name=value;domain=.google.com";
In this way, all hosts under google.com can access the cookie.
Comprehensive example: Constructing a universal cookie processing function
The cookie processing process is relatively complex and has certain similarities. Therefore, several functions can be defined to complete the general
operations of cookies, thereby achieving code reuse. Common cookie operations and their function implementations are listed below.
1. Add a cookie: addCookie(name, value, expiresHours)
This function receives 3 parameters: cookie name, cookie value, and how many hours it will expire. It is agreed here that when expiresHours is
0, no expiration time is set, that is, the cookie disappears automatically when the browser is closed. The function is implemented as follows:
2. Get the cookie value of the specified name: getCookie(name)
This function returns the cookie value named name. If it does not exist, it returns empty. Its implementation is as follows:
3. Delete the cookie with the specified name: deleteCookie(name)
This function can delete the cookie with the specified name