JavaScript calls Cookies

Source: Internet
Author: User

Cookies

used to store information and interact with the server, so cookies can be used by the front- end Backend

It is worth mentioning that although the cookie is stored as a file on the client computer,

However, the browser will only allow the website that created the cookie to access its own cookies.

The following experiment takes the form as an example

User:<type= "text"  ID= "user"/><  /> Password: <  type= "password"  onblur= "set ()"  ID= "Pass"  /></>

The cookie is a property of the document object and supports write and read.

To create a cookie syntax format:

Document.cookie = "Name=value"

After the user has entered the account password, the value of the cookie is stored through the Blur event.

        function  set () {            // If the value of the form is not empty, then save its value if            (user.value! = "" &&pass.value!= "") {                document.cookie= "username =" + user.value;                Document.cookie= "Password =" +   pass.value;             }
}

To view the value of a cookie that has already been stored on a webpage:

alert (document.cookie); The result is username=hello;password=123456

alert (typeof document.cookie); Result is a string

To obtain the cookie value for the cookie name:

functionGet_cookies_value (CNAME) {varCOO =Document.cookie; //the next line has a detail: if you do not add =, you cannot avoid the situation when the COO value is equal to the COO name.    varindex = coo.indexof (cname + "=")
//if the name exists if(Index!=-1){ //gets the start position of the value varIndex_start = index + cname.length + 1;//equals sign occupies one bit varIndex_end = Coo.indexof (";", Index_start); if(Index_end = =-1){ //If there is no semicolon, the value is the last, so the end index is the COO lengthIndex_end=coo.length; } //intercept the value and return varValue =coo.substring (index_start,index_end); returnvalue; }}

You can then insert the cookie value into the form when the page is loaded:

var user = document.getElementById ("user"); var pass = document.getElementById ("pass"); if (document.cookie!= "") {    User.value= Get_cookies_value ("user");     Pass.value= get_cookies_value ("pass");}

JavaScript calls Cookies

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.