1 //Cookie Section2 //Set Cookies3 //Note the CNAME incoming string type, such as: Setcookie (' first ');4 functionSetcookie (cname,cvalue,exdays) {5 varD =NewDate ();6D.settime (D.gettime () + (exdays*24*60*60*1000));7 varexpires = "expires=" +d.togmtstring ();8Document.cookie = cname+ "=" +cvalue+ "; "+expires;9 }Ten //Get Cookies One functionGetCookie (CNAME) { A varName = CNAME + "="; - varCA = Document.cookie.split ('; ')); - for(vari=0; i<ca.length; i++) { the varc =Ca[i].trim (); - if(C.indexof (name) ==0)returnc.substring (name.length,c.length); - } - return""; + } - //Check Cookies + functionCheckcookie () { A varUser=getcookie ("username"); at if(user!= ""){ -Alert ("Welcome again" +user); - } - Else { -user = prompt ("Please enter your name:", "" "); - if(user!= "" && user!=NULL){ inSetcookie ("username", user,30); - } to } + } - //Delete a single cookie the functionClearCookie (name) { *Setcookie (Name, "",-1); $ }Panax Notoginseng //Delete all Cookies - functionClearallcookie () { the varKeys=document.cookie.match (/[^ =;] +(?=\=)/g); + if(keys) { A for(vari = keys.length; i--;) thedocument.cookie=keys[i]+ ' =0;expires= ' +NewDate (0). toUTCString () + } -}
Use of cookies