"JavaScript" JS cross-domain settings and fetch cookies

Source: Internet
Author: User
Tags setcookie

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:

  1. <script type="Text/javascript" >
  2. function Setcookie (c_name,value,expiredays) {
  3. var exdate=New Date ();
  4. Exdate.setdate (Exdate.getdate () +expiredays);
  5. Alert (Exdate.getdate () +expiredays);
  6. document.cookie=c_name+ "=" +escape (value) + ((expiredays==null)?  " ": "; expires=" +exdate.togmtstring ()) +";p ath=/;d omain=b.com";
  7. }
  8. window.onload=function () {
  9. Setcookie ("Listallwjh","Sfwjh");
  10. Alert ("The cookie is set successfully!  ");
  11. }
  12. </script>

C.b.com Station built a.html. The key code is as follows

  1. <script>
  2. function GetCookie (c_name) {
  3. if (document.cookie.length>0)
  4. {
  5. C_start=document.cookie.indexof (c_name + "=");
  6. if (c_start!=-1)
  7. {
  8. C_start=c_start + c_name.length+1;
  9. C_end=document.cookie.indexof (";", C_start);
  10. if (c_end==-1) c_end=document.cookie.length;
  11. Return unescape (document.cookie.substring (c_start,c_end));
  12. }
  13. }
  14. return "";
  15. }
  16. window.onload=function () {
  17. var c_name="Listallwjh";
  18. if (GetCookie ("listallwjh") =null) {
  19. Alert (GetCookie ("Listallwjh"));
  20. }
  21. }
  22. </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&rsquo;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

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.