Simple use of cookies

Source: Internet
Author: User

The server can filter and regularly maintain the information contained in Cookies to determine the status during HTTP transmission. The most typical application of Cookies is to determine whether a registered user has logged on to the website. The user may be prompted to determine whether to keep the user information during the next visit to simplify the logon procedures, these are the functions of Cookies. Another important application scenario is "Shopping Cart. Users may select different items on different pages of the same website within a period of time. These information will be written into Cookies so that information can be extracted during the final payment.



// Create a Cookiefunction createCookie (userName, password) {var date = new Date (); date. setTime (date. getTime () + 60*1000*60*24); // Save the document for one day. cookie = "userName =" + escape (userName) + "; expires =" + date. toUTCString (); // The escape Function encodes the String, and setTime is the current document of the Cookie. cookie = "password =" + escape (password) + "; expires =" + date. toUTCString (); alert (document. cookie);} // obtain the Cookie data function getCookie (value) {var cookieString = document. cookie; var start = cookieString. indexOf (value + "="); // locate the value position. if (start =-1) return null; start + = value. length + 1; // value length + 1, that is, = the position behind var end = cookieString. indexOf (";", start); // The length of valuer + 1 lookup; position if (end =-1) // that is, there is only one Cookie, so no; return cookieString. substring (start); return cookieString. substring (start, end) ;}// Delete Cookiefunction delCookie () {var date = new Date (); var user = getCookie ("userName "); var pwd = getCookie ("password"); date. setTime (date. getTime ()-1); // reduce the current time to 1 ms from the current system time to a negative value, and the Cookie will expire in the system, the document will be destroyed. cookie = "userName =" + escape (user) + ";" + "expires =" + date. toUTCString (); document. cookie = "password =" + escape (pwd) + ";" + "expires =" + date. toUTCString ();}


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.