JS Read cookies information (record user name) _javascript tips

Source: Internet
Author: User
First, after the user submits the comment, lets the client cookies record the correlation value, for example: The ASP under the cookie assignment method uses the following statement:
Copy Code code as follows:

Response.Cookies ("username") = "Name"
Response.Cookies ("username"). expires=date+30


Assign value to cookies through the above ASP program. How do I read this cookie in the static page HTML and display it in the user? Because it is generated HTML, we can no longer use the ASP program to read this cookie, need to read this cookie through JS, and given to the corresponding input value.

Use JS to read cookies and assign values to the following code:
Copy Code code as follows:

<script type= "Text/javascript" >
JS Get Cookie
var acookie=document.cookie.split (";");
function Getck (sname)
{//Get a single cookie
for (Var i=0;i<acookie.length;i++) {
var arr=acookie[i].split ("=");
if (Sname==arr[0]) {
if (arr.length>1)
Return unescape (arr[1]);
Else
Return "";
}}
Return "";
}
Assign values to the input in the corresponding form
DOCUMENT.FORM_NAME.INPUT_NAME.VALUE=GETCK ("username");
</script>

In this way, the static page can be successfully read from the client cookies information, and display.

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.