How to use cookies to record temporary visitor information in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces how JavaScript uses cookies to record temporary visitor information. It involves javascript cookie operations and is very useful, for more information about how to use cookies to record temporary visitor information, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

Here, the user will prompt the user to enter a nickname when accessing the webpage for the first time, and then write the cookie. When the user comes again, the cookie information will be read and the user nickname will be extracted.

《script》function getCookie(c_name){var c_value = document.cookie;var c_start = c_value.indexOf(" " + c_name + "=");if (c_start == -1) { c_start = c_value.indexOf(c_name + "="); }if (c_start == -1) { c_value = null; }else { c_start = c_value.indexOf("=", c_start) + 1; var c_end = c_value.indexOf(";", c_start); if (c_end == -1)  {  c_end = c_value.length;  } c_value = unescape(c_value.substring(c_start,c_end)); }return c_value;}function setCookie(c_name,value,exdays){var exdate=new Date();exdate.setDate(exdate.getDate() + exdays);var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());document.cookie=c_name + "=" + c_value;}function checkCookie(){var username=getCookie("username");if (username!=null && username!="") { alert("Welcome again " + username); }else { username=prompt("Please enter your name:",""); if (username!=null && username!="")  {  setCookie("username",username,365);  } }}《script》

I hope this article will help you design javascript programs.

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.