A cookie is a variable stored on a visitor's computer. This cookie is sent whenever a page is requested by the same computer through a browser. You can use JavaScript to create and retrieve the value of a cookie. This article mainly JS how to read the cookie and domain settings.
AD:
In Javascript scripts, a cookie is actually a string attribute. When you read the value of a cookie, you get a string that contains the names and values of all the cookies used in the current Web page. Each cookie has four properties in addition to the name and value values. These properties are: Expires expiration time, path path, domain realm, and secure security.
expires– Expiration time. Specifies the lifetime of the cookie. The specific value is the expiration date. You must use this property if you want the cookie to exist longer than the current browser session. When the expiration date is over, the browser can delete the cookie file without any effect.
path– path. Specifies the Web page that is associated with the cookie. The value can be a directory, or a path. If http://www.zdnet.com/devhead/index.html establishes a cookie, then all pages in the http://www.zdnet.com/devhead/directory, and the pages in any subdirectory below the directory can access this cookie. This means that any page in Http://www.zdnet.com/devhead/stories/articles can access the cookie created by http://www.zdnet.com/devhead/index.html. However, what if http://www.zdnet.com/zdnn/needs to access the Cookes of the http://www.zdnet.com/devhead/index.html settings? At this point, we want to set the path property of the cookie to "/".
When specifying a path, cookies can be shared by all Web pages that have the same path in the URL from the same server. Now let's look at another example: if you want http://www.zdnet.com/devhead/filters/and http://www.zdnet.com/devhead/stories/to share cookies, set path to "/ Devhead ".
domain– domain . Specifies the associated Web server or domain. The value is a domain name, such as Zdnet.com. This is an extension to the Path property. What if we want catalog.mycompany.com to have access to cookies set by shoppingcart.mycompany.com? We can set the domain property to "mycompany.com" and set the Path property to "/". The cookie domain attribute cannot be set to a different value than the domain of the server where it is set.
secure– safe. specifies how the value of the cookie is passed between the user and the Web server through the network. The value of this property is either "secure", or null. By default, this property is empty, which means that data is passed using an unsecured HTTP connection. If a cookie is marked secure, it passes data to and from the Web server over HTTPS or other security protocols. However, setting the secure property does not mean that others cannot see the cookie that is stored locally on your machine. In other words, setting the cookie to secure only guarantees that the data transfer process between the cookie and the Web server is encrypted, while the cookie file stored locally is not encrypted. If you want to encrypt your local cookie, you have to encrypt your data yourself.
Example: A.b.com and c.b.com two stations, established as follows:
A.b.com Station built a.html. The key code is as follows:
- <script type="Text/javascript" >
- function Setcookie (c_name,value,expiredays) {
- var exdate=New Date ();
- Exdate.setdate (Exdate.getdate () +expiredays);
- Alert (Exdate.getdate () +expiredays);
- document.cookie=c_name+ "=" +escape (value) + ((expiredays==null)? " ": "; expires=" +exdate.togmtstring ()) +";p ath=/;d omain=b.com";
- }
- window.onload=function () {
- Setcookie ("Listallwjh","Sfwjh");
- Alert ("The cookie is set successfully! ");
- }
- </script>
C.b.com Station built a.html. The key code is as follows
- <script>
- function GetCookie (c_name) {
- if (document.cookie.length>0)
- {
- C_start=document.cookie.indexof (c_name + "=");
- if (c_start!=-1)
- {
- C_start=c_start + c_name.length+1;
- C_end=document.cookie.indexof (";", C_start);
- if (c_end==-1) c_end=document.cookie.length;
- Return unescape (document.cookie.substring (c_start,c_end));
- }
- }
- return "";
- }
- window.onload=function () {
- var c_name="Listallwjh";
- if (GetCookie ("listallwjh") =null) {
- Alert (GetCookie ("Listallwjh"));
- }
- }
- </script>
These two are also set, one is to take, as long as the two rules to ensure the same, on the line. Run, Result: ok!
Note: These two methods can sometimes be written together, first to determine whether a cookie exists, in the creation.
Friendship Information:
First, the browser allows each domain name contains the number of cookies: Microsoft noted that Internet Explorer 8 added a cookie limit of 50 per domain name, but IE7 also seemed to allow 50 cookies per domain name (color= "#006d A3 "> Update to Internet explorer’s Cookie Jar").
Firefox has a limit of 50 cookies per domain name. Opera has a limit of 30 cookies per domain name. Safari/webkit seems to have no cookie restrictions. However, if there are many cookies, the header size will exceed the server's processing limit, which can cause errors to occur. Note: Each domain name cookie limit of 20 will no longer be correct!
Second, when a lot of cookies are set, how the browser responds. In addition (can set all cookies, regardless of quantity), there are two methods: least recently Used (least recently Used (LRU)) method: When the cookie has reached the limit, automatically kicks apart the oldest cookie to give some space to the latest cookie. Internet Explorer and Opera Use this method.
Firefox is unique: Although the last set of cookies is always retained, it seems to randomly determine which cookies are retained. There seems to be no plan (recommended: Do not exceed the Cookie limit in Firefox).
Third, the total size of the cookie between different browsers is also different: Firefox and Safari allow up to 4,097 bytes of cookies, including the name, value, and equal sign. Opera allows up to 4,096 bytes of cookies, including: Name, value, and equal sign. Internet Explorer allows up to 4,095 bytes of cookies, including: Name, value, and equal sign. Note: Multibyte characters are calculated as two bytes. In all browsers, any cookie size exceeding the limit is ignored and will never be set.
When a visitor visits the page for the first time, he or she may fill out his/her name. The name is stored in a cookie. When visitors visit the site again, they receive a welcome word like "Welcome John doe!". And the name is retrieved from the cookie. This is a cookie and I hope this article will help you.
Http://developer.51cto.com/art/201104/256793.htm