JS cross-domain setting and fetching cookies (ii)

Source: Internet
Author: User
Tags getdate setcookie

A cookie is a variable that is stored in a visitor's computer. This cookie is sent whenever the same computer requests a page through the browser. You can use JavaScript to create and retrieve a cookie's value. This article mainly JS how to read cookies and domain settings.



In Javascript scripts, a cookie is actually a string property. When you read the value of the cookie, you get a string that contains the name and value of all 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 field, and secure security.

expires– Expiration time. Specifies the lifetime of the cookie. The exact value is the expiration date. You must use this property if you want the lifetime of the cookie to exceed the current browser session time. When the expiration date is passed, the browser can delete the cookie file without any effect.

path– path. Specifies the Web page 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, This cookie is accessible to pages in any subdirectory below the directory. This means that any page in the Http://www.zdnet.com/devhead/stories/articles can access cookies created by http://www.zdnet.com/devhead/index.html. However, what to do if http://www.zdnet.com/zdnn/needs to access the Cookes of the http://www.zdnet.com/devhead/index.html setting. At this point, we will set the cookie's path attribute to "/".

When you specify a path, all Web pages that come from the same server with the same path in the URL can share cookies. Now look at another example: if you want http://www.zdnet.com/devhead/filters/and http://www.zdnet.com/devhead/stories/to share cookies, set the path to "/ Devhead ".

domain– domain . Specifies the associated Web server or domain. Values are domain names, such as zdnet.com. This is an extension of the path Path property. What if we want catalog.mycompany.com to be able to access cookies set by shoppingcart.mycompany.com? We can set the domain property to "mycompany.com" and set the Path property to "/". You cannot set the cookie domain attribute 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 over the network. The value of this property is either "secure" or empty. By default, this property is null, which means that data is passed using an unsecured HTTP connection. If a cookie is marked secure, it passes data between it and the Web server through HTTPS or other security protocols. However, setting the secure attribute does not mean that other people cannot see cookies stored locally on your machine. In other words, setting the cookie to secure only guarantees that the cookie is encrypted with the data transfer process between the Web server, while the locally stored cookie file is not encrypted. If you want to encrypt the local cookie, you have to encrypt the data yourself.

Example: A.b.com and c.b.com two stations, established in the following manner:

A.b.com Station under the building 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 ("Cookie setting is successful.) ");  
    }  

C.b.com Station under the building 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 taken, as long as the guarantee two rules, on the line. Run, Result: OK.

Note: These two methods can sometimes be written together to determine whether a cookie exists in the creation.

Friendship Information:

The number of cookies that the browser allows for each domain name: Microsoft points out that Internet Explorer 8 increases the cookie limit to 50 for each domain name, but IE7 also seems to allow 50 cookies per domain name (color= "#006d A3 "> Update to Internet explorer&rsquo;s Cookie Jar").

Firefox limits each domain cookie to 50. Opera has a limit of 30 cookies per domain. Safari/webkit doesn't seem to have a cookie limit. However, if there are many cookies, the header size exceeds the limit of the server's processing, 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 up, how the browser to respond. In addition to (you can set all cookies, regardless of number), there are two methods: least recently Used (least recently Used (LRU)) method: When the cookie has reached the limit, automatically kicked out 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 is different between browsers: Firefox and Safari allow cookies up to 4,097 bytes, including name, value (value), and equals sign. Opera allows cookies up to 4,096 bytes, including: Name, value (value), and equals sign. Internet Explorer allows cookies up to 4,095 bytes, including: Name, value (value), and equals sign. Note: Multibyte characters are calculated to be 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 in his or her name. The name is stored in the 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.



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.