Simple use of cookies in JavaScript

Source: Internet
Author: User
Tags date current time end return

Usually, the website designer uses the backstage database to achieve the above goal. When the user first accesses the Web site, the site creates a new ID in the database and sends the ID to the user via a cookie. When the user visits again, the website adds 1 to the counter of the user ID, gets the user's visit number or determines whether the user is a new user or an old user.

The server can use cookies to contain information about the arbitrary nature to filter and regularly maintain this information to determine the status in the HTTP transmission. The most typical use of cookies is to determine whether registered users have logged on to the site, users may be prompted, whether the next time to enter this site to retain user information to simplify the login procedures, these are the function of cookies. Another important application is the "shopping cart" process. Users may select different items on different pages of the same Web site for a period of time, which will be written to cookies to extract information at the final payment. 


// Create a cookie
function createCookie (userName, password) {
  
    var date = new Date ();
    date.setTime (date.getTime () + 60 * 1000 * 60 * 24); // Save the time of day
    document.cookie = "userName =" + escape (userName) + "; expires =" + date.toUTCString ();
// escape function encodes String, setTime is the lifetime of the cookie
    document.cookie = "password =" + escape (password) + "; expires =" + date.toUTCString ();
    alert (document.cookie);
}
// Get cookie data
function getCookie (value) {
     var cookieString = document.cookie;
        var start = cookieString.indexOf (value + "="); // Find the position of value
        if (start ==-1)
            return null;
        start + = value.length + 1; // length of value + 1, which is the position after =
        var end = cookieString.indexOf (";", start); // length of valuer +1 to find;
        if (end ==-1) // In other words, there is only one cookie so there is no;
            return cookieString.substring (start);
         return cookieString.substring (start, end);
  
}
// Delete cookies
function delCookie () {
  
    var date = new Date ();
    var user = getCookie ("userName");
    var pwd = getCookie ("password");
    date.setTime (date.getTime ()-1);
// Decrease the current time -1 milliseconds with the current system time and become a negative value, then this cookie expires when stored in the system and will be destroyed
    document.cookie = "userName =" + escape (user) + ";" + "expires =" + date.toUTCString ();
    document.cookie = "password =" + escape (pwd) + ";" + "expires =" + date.toUTCString ();
}

how php gets 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.