Tips on using Javascript_javascript for Document.cookie

Source: Internet
Author: User

Document.cookie= "userid=828";
If you want to store more than one name/value pair at a time, you can use a semicolon plus a space (; ) separated, for example:
Document.cookie= "userid=828; Username=hulk ";
You cannot use semicolons (;), commas (,), Equals (=), and spaces in the name or value of a cookie. Do it in the name of the cookie
This is easy to do, but the value to be saved is indeterminate. How do you store these values? method is to use the escape () function to edit
Code that can use hexadecimal notation for some special symbols, such as a space that will be encoded as "20%" so that it can be stored in
Cookie value, and the use of this scheme can also avoid the appearance of Chinese garbled. For example:
Document.cookie= "str=" +escape ("I love Ajax");
Equivalent:
Document.cookie= "Str=i%20love%20ajax";
When you use Escape () encoding, you need to use unescape () to decode after the value is fetched to get the original cookie value.
This has been introduced before.
Although Document.cookie looks like an attribute, it can be assigned a different value. But it's not the same as the general properties that change
Its assignment does not mean losing the original value, such as executing the following two statements consecutively:
Document.cookie= "userid=828";
Document.cookie= "Username=hulk";
The browser will maintain two cookies, respectively UserID and username, so give Document.cookie a value like a
Lines that are similar to these statements:
Document.addcookie ("userid=828");
Document.addcookie ("Username=hulk");
In fact, the browser is in this way to set cookies, if you want to change the value of a cookie, you just have to reassign
Values, such as:
Document.cookie= "userid=929";
This sets the cookie value named UserID to 929.
Get the value of a cookie
The following describes how to get the value of a cookie. The value of a cookie can be obtained directly by Document.cookie:
var Strcookie=document.cookie;
This will get a string of multiple name/value pairs separated by semicolons that include all cookies under that domain name
。 For example:
Figure 7.1 shows the cookie value for the output. This shows that you can only get all the cookie values at once, and you cannot specify cookies
Name to get the specified value, which is the most troublesome part of handling cookie values. Users must analyze this string themselves to
Gets the specified cookie value, for example, to get the value of the UserID, which can be achieved by:
So you get the value of a single cookie.
In a similar way, you can get the value of one or more cookies, and the main trick is still the related operations of strings and arrays.
Set expiration date for cookie
Until now, all cookies are a single session cookie, that is, the cookies will be lost when the browser is closed, and in fact this
Some cookies are stored in memory only, not the corresponding hard disk files.
In actual development, cookies often need to be saved for long periods of time, such as saving the user's login status. This can be implemented with the following options
Is:
Document.cookie= "userid=828; Expires=gmt_string ";
Where Gmt_string is a time string in GMT format, this statement is to set the UserID cookie to
The expiration time that gmt_string represents, beyond which the cookie disappears and is inaccessible. For example, if you want the cookie
When set to expire after 10 days, this can be achieved:

Delete Cookies
To delete a cookie, you can set its expiration time to a past time, for example:
Specify the path to the cookie that can be accessed
By default, if a cookie is created on a page, other pages in the same directory as the page can also be accessed
The cookie. If there are subdirectories under this directory, they can also be accessed in subdirectories. For example in
Cookies created in the www.xxxx.com/html/a.html can be www.xxxx.com/html/b.html or
Www.xxx.com/html/some/c.html, but cannot be accessed by www.xxxx.com/d.html.
To control which directories a cookie can access, you need to set the cookie using the path parameter, which is the following syntax:
Document.cookie= "Name=value; Path=cookiedir ";
Where Cookiedir represents a directory that can access cookies. For example:
Document.cookie= "userid=320; Path=/shop ";
means that the current cookie can only be used in the shop directory.
If you want the cookie to be available throughout the Web site, you can designate Cookie_dir as the root directory, for example:
Document.cookie= "userid=320; path=/";

Specifies the host name that can access the cookie
Similar to paths, host names refer to different hosts under the same domain, for example: www.google.com and gmail.google.com
is two different host names. By default, cookies created in one host cannot be accessed under another host,
However, you can control it by using the domain parameter, which has the following syntax format:
Document.cookie= "Name=value; Domain=cookiedomain ";
Take Google, for example, to achieve cross host access, which can be written as:
Document.cookie= "name=value;domain=.google.com";
In this way, the cookie is accessible to all hosts under Google.com.

Comprehensive example: Constructing a generic cookie handler function
The processing of cookies is more complex and has a certain similarity. So you can define a few functions to complete the cookie's generic
Operation, which enables the reuse of code. The common cookie actions and their function implementations are listed below.
1. Add a Cookie:addcookie (name,value,expireshours)
The function receives 3 parameters: The cookie name, the cookie value, and how many hours after the expiration. Here the agreement expireshours for
0 o'clock does not set the expiration time, that is, when the browser closes the cookie automatically disappears. This function is implemented as follows:
2. Gets the cookie value for the specified name: GetCookie (name)
The function returns a cookie value named name and, if it does not exist, returns NULL, which is implemented as follows:
3. Deletes the Cookie:deletecookie (name) of the specified name
This function can delete a cookie with the specified name

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.