Static page Use JS to read cookies Remember user information _ Application Tips

Source: Internet
Author: User
Static page because of its stability fast, it does give SE, the user and webmaster brought convenience. But sometimes, you need to remember the user's information, such as the user left a comment, the next time, you should remember the user's information, do not have to enter again.
For users, they can improve their sense of belonging and familiarity. How to achieve it?
First of all, we need to submit a comment after the user to give the client a number of related cookies value, this is very simple, directly in the Comment submission page to assign value on the ASP, the cookie simple assignment method using the following statement:
Copy Code code as follows:

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


By using the cookies in the above ASP program, we successfully write the user's cookie information to our website. The next thing to do is how to read this cookie in the static page HTML and display it in front of 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's system we have been assigned cookies information, and displayed. is not very simple, hehe. If you have other ideas, welcome to discuss it with me.

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.