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)