Use javascript For document. cookie

Source: Internet
Author: User

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. In the cookie name

This is easy, but the value to be saved is uncertain. How can we store these values? The method is to use the escape () function for coding.

It can represent some special symbols in hexadecimal notation. For example, a space is encoded as "20%", which can be stored in

Cookie value, and the use of this scheme can also avoid Chinese garbled characters. For example:
Document. cookie = "str =" + escape ("I love ajax ");
Equivalent:
Document. cookie = "str = I % 20 love % 20 ajax ";
After escape () encoding is used, you must use unescape () to decode the value before obtaining the original cookie value,

This is already described earlier.
Although document. cookie looks like an attribute, different values can be assigned. But it is different from the general property, changing

Its value assignment does not mean that the original value is lost. For example, the following two statements are executed consecutively:
Document. cookie = "userId = 828 ";
Document. cookie = "userName = hulk ";
In this case, the browser will maintain two cookies, namely userId and userName.

The following statements are used:
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 re-assign

Value, 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:

Figure 7.1 shows the output cookie value. It can be seen that all cookie values can be obtained only once, and no cookie can be specified.

Name to obtain the specified value, which is the most troublesome part to process the cookie value. You must analyze this string by yourself.

Obtain the specified cookie value. For example, to obtain the value of userId, you can do this:

In this way, the value of a single cookie is obtained.

A similar method can be used to obtain the values of one or more cookies. The main technique is the operations related to strings and arrays.

Set the cookie end date
So far, all cookies are single-session cookies, that is, these cookies will be lost after the browser is closed. In fact, this

Some cookies are only stored in the memory, but the corresponding hard disk files are not created.
In actual development, cookies often need to be saved for a long time, for example, the user login status. This can be implemented using the following options:

Now:
Document. cookie = "userId = 828; expires = GMT_String ";
GMT_String is a time string in GMT format. This statement sets the cookie userId

GMT_String indicates the expiration time. After this time, the cookie will disappear and cannot be accessed. For example

It is set to expire after 10 days, which can be implemented as follows:


Delete cookie
To delete a cookie, you can set its expiration time to a previous time, for example:

Specify the path to the cookie
By default, if a cookie is created on a page, other pages in the directory where the page is located can also be accessed.

This cookie. If there are subdirectories in this directory, they can also be accessed in the subdirectory. For example

Cookiecreated in www.xxxx.com/html/a.html can be www.xxxx.com/html/ B .htmlor

Www.xxx.com/html/some/c.html, but cannot be accessed by www.xxxx.com/d.html.
To control the directories that can be accessed by cookies, you need to use the path parameter to set cookies. The syntax is as follows:
Document. cookie = "name = value; path = cookieDir ";
CookieDir indicates the directory that can access cookies. For example:
Document. cookie = "userId = 320; path =/shop ";
Indicates that the current cookie can only be used in the shop directory.
To make the cookie available on the entire website, you can specify cookie_dir as the root directory. For example:
Document. cookie = "userId = 320; path = /";


Specifies the Host Name of the cookie that can be accessed.
Similar to the path, host names refer to different hosts in the same domain, such as www.google.com and gmail.google.com.

Is two different host names. By default, the cookies created on one host cannot be accessed on another host,

However, you can use the domain parameter to control it. The syntax format is as follows:
Document. cookie = "name = value; domain = cookieDomain ";
Take google as an example. To achieve cross-host access, you can write as follows:
Document. cookie = "name = value; domain = .google.com ";
In this way, all hosts under google.com can access this cookie.


Comprehensive example: construct common cookie processing functions
Cookie processing is complex and has a certain degree of similarity. Therefore, you can define several functions to implement the general usage of cookies.

To reuse the code. Common cookie operations and their function implementations are listed below.
1. Add a cookie: addCookie (name, value, expiresHours)
This function receives three parameters: cookie name, cookie value, and how many hours after expiration. The expiresHours is

When the browser is disabled, the cookie disappears automatically. 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 NULL. Its implementation is as follows:

3. Delete the cookie with the specified name: deleteCookie (name)
This function deletes the cookie with the specified name.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.