JavaScript operation Cookie (add, delete, change, check)

Source: Internet
Author: User

1 //1. Get the value of a cookie with the specified name objname2 functionGetCookie (objname) {3     varArrstr = Document.cookie.split (";");//between the various cookies;4      for(vari=0;i<arrstr.length;i++){5         vartemp = arrstr[i].split ("=");//the cookie name and cookie value are separated by =6         if(Temp[0] = =objname)7             returnUnescape (temp[1]);8     } 9     return""; Ten } One  A //2. Add a cookie value to the browser's built-in Documnet.cookie object - functionAddcookie (objname,objvalue,objhours) { -     varstr = objname + "=" +Escape (objvalue); the     if(Objhours > 0) {//If the expiration time is not set (i.e. when objhours<=0), the cookie will be automatically deleted when the browser is closed -         varDate =NewDate (); -         varms = objhours*3600*1000;  -Date.settime (Date.gettime () +ms); +str + = "; Expires= "+date.togmtstring (); -    } +Document.cookie =str; A } at  -  //3. Set the value of the cookie named objname and set the expiration time to 30 days - functionSetcookie (Objname,objvalue) { -     vardays = 30;//indicatesThis cookie will be saved for 30 days -     varExp =NewDate (); -Exp.settime (Exp.gettime () + days*24*60*60*1000); inDocument.cookie = name + "=" + Escape (value) + "; expires=" +exp.togmtstring (); - } to  + //4. Delete the cookie named objname - functionDelcookie (objname) { the     varExp =NewDate (); *Exp.settime (Exp.gettime ()-1); $     varValue=GetCookie (objname);Panax Notoginseng     if(value!=NULL) { -document.cookie= name + "=" +value+ "; expires=" +exp.togmtstring (); the     } +}

JavaScript operation Cookie (add, delete, change, check)

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.