This is the last obstacle to understanding cookies: by default, cookies can only be stored in
Web page reading with the cookie configured in the same path on the same Web server.
For example
Http://chimp.webmonkey.com/food/bananas/banana_puree.htm"
A piece of Javascript asks the user's name. You may need
For example, to access a given name on the home page, you must set
Cookie path. path "path" is used to set the maximum number
Top-level directory. Set the Cookie Path to the top-level directory of Your webpage.
To allow all webpages in this directory to access this cookie.
Method: Add Path =/to your cookie. If you only want to make the "food" Directory
You can use this cookie to add Path =/food:
Some websites have many small domain names.
"Chimp.webmonkey.com," "gorilla.webmonkey.com," and
"Ape.webmonkey.com." The domain name has a webpage. By default, only
This cookie can be read from webpages in the "chimp.webmonkey.com" domain. If
You want all machines under "webmonkey.com" to read this cookie.
You must add "Domain = webmonkey.com" to the cookie ".
To set a cookie in
Http://chimp.webmonkey.com/food/bananas/banana_puree.htm"
In addition, all web pages of Web monkeys can use it. We can do this:
Code
Function setCookie ()
{
VaR the_name = prompt ("What's your name? ","");
VaR the_cookie = "cookie_puss =" + escape (the_name) + ";";
VaR the_cookie = the_cookie + "Path = /;";
VaR the_cookie = the_cookie + "Domain = webmonkey.com ;";
Document. Cookie = the_cookie;
}
Now we have learned more about cookies.
========================================================== ================
Although a cookie is created by a webpage, it is not only the webpage that creates the cookie that can read the cookie. By default, the cookie can be read by all webpages in the same directory or subdirectory as the webpage that creates the cookie. However, if there are subdirectories in this directory, you need to use the path parameter to set the cookie to make them accessible in the subdirectory. The syntax is as follows:
Document. Cookie = "name = value; Path = cookiedir ";
To make the cookie available on the entire website, you can specify cookiedir as the root directory, for example:
Document. Cookie = "userid = 320; Path = /";
All of the above refer to access in the same directory. However, if you want to access in different virtual directories, you must try another way to solve this problem. However, path cannot solve the problem of cookie Access in different domains. By default, only webpages on the same Web server with the configured cookie can access the cookies created on the webpage. However, you can use the domain parameter to control it. The syntax format is as follows:
Document. cookie = "name = value; domain = cookieDomain ";
The following code demonstrates how to set the cookie Path and domain.
Code
<Script lnguage = "javascript"> // JavaScript code start tag
Function setCookie () // custom function
{
VaR the_name = prompt ("Enter the cookie value to be set," "); // an information box
Var the_cookie = "cookie_puss =" + escape (the_name) + ";"; // compile the cookie key and Value
Var the_cookie = the_cookie + "path =/;"; // set the cookie path
Var the_cookie = the_cookie + "domain = localhost;"; // set the cookie domain
Document. cookie = the_cookie; // write the information to the cookie variable.
Alert ("set successfully! ") // Finally, the user is prompted that the setting is successful.
}
SetCookie (); // call a function
</Script>
Note: you must pay attention to the path setting method when using it. If the path information is incorrect, it cannot be accessed.