JavaScript implements the method of setting, fetching, and deleting cookies _javascript tips

Source: Internet
Author: User

The examples in this article describe the methods that JavaScript implements to set up, get, and delete cookies. Share to everyone for your reference. The implementation method is as follows:

* * Set the corresponding value of the key * Value:cookie of the cookie * * * * * * * * Name:cookie * Expires:cookie The valid time * path: Specifies the path of the accessible cookie * DOMA  
  In: Specify host name for accessible cookies * Secure: Security/function Setcookie (name,value,expires,path,domain,secure) {//cookie key value pair 
  var str = name + "=" + escape (value); 
    Set the lifetime of the cookie, in hours if (expires > 0) {var date = new Date (); 
    var ms = Expires * 3600 * 1000; 
    Date.settime (Date.gettime () + ms); str + = "; 
  Expires= "+ date.togmtstring (); } if (path) {str + =; 
  Path= "+ path; } if (domain) {str =; 
  domain= "+ domain; } if (secure) {str =; 
  Secure "; 
} document.cookie = str; 
  * * * Get the key/function GetCookie (cookie_name) {var value = null for cookie * *COOKIE_NAME:COOKIE;  
  var allcookies = Document.cookie;  
  var cookie_pos = Allcookies.indexof (cookie_name);  
  If an index is found, it means that the cookie exists,//otherwise, it does not exist.  
    if (Cookie_pos!=-1) {//Place the Cookie_pos at the beginning of the value, just add 1 to the value.Cookie_pos + = cookie_name.length + 1;  
    var cookie_end = Allcookies.indexof (";", Cookie_pos);  
    if (cookie_end = = 1) {cookie_end = Allcookies.length;  
  } value = Unescape (allcookies.substring (Cookie_pos, cookie_end));  
return value; 
  * * * Delete cookie * *cookie_name:cookie key/function Delcookie (cookie_name) {var exp = new Date (); 
  Exp.settime (Exp.gettime ()-1); 
  var value = GetCookie (Cookie_name); 
  if (value) {document.cookie= cookie_name + "=" + Value + "; expires=" + exp.togmtstring ();

 } 
}

The

wants this article to help you with your JavaScript programming.

Related Article

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.