Common cookies in JS operation __js

Source: Internet
Author: User
Create a new cookie. When hours is an empty string, the lifetime of the cookie ends with the browser session.
Hours is the number 0 o'clock, a failed cookie is created that overwrites the already established cookie with the same path (if the cookie exists).
    function Setcookie (name,value,hours,path) {var name = Escape (name);
    var value = Escape (value);
    var expires = new Date ();
    Expires.settime (Expires.gettime () + hours*3600000);
    Path = Path = ""? "": ";p ath=" + path; _expires = (typeof hours) = = "string"?
    "": "; expires=" + expires.toutcstring ();
Document.cookie = name + "=" + value + _expires + path;
    //Get the cookie value function Getcookievalue (name) {//Read cookie property, which returns all of the document's cookie var allcookies = Document.cookie;
    Finds the start position of the cookie named name = = "=";
    var pos = allcookies.indexof (name); If a cookie with that name is found, it is extracted and used with its value if (POS!=-1) {//If the POS value of-1 indicates that the search for "version=" is missing                  var start = pos + name.length;        The position where the cookie value starts var end = Allcookies.indexof (";", start); Search for the first ";" from where the cookie value starts.
   Position, that is, the end of the cookie value     if (end = = 1) end = Allcookies.length;  If the end value is-1, only one cookie in the cookie list is var value = allcookies.substring (start,end);                           Extract the value of the cookie return unescape (value);                                             Decode it} else return "";
    Search failed, return empty string}//Delete cookie function Deletecookie (name,path) {var name = Escape (name);
    var expires = new Date (0);
    Path = Path = ""? "": ";p ath=" + path;
Document.cookie = name + "=" + "expires=" + expires.toutcstring () + path;
 }


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.