The cookies in JavaScript are primarily stored on the client computer and are used to store information about the site that has been visited, with a maximum cookie of approximately 4k. The following instances are primarily used to save data when the page is refreshed, as follows:
<HTML><Head><title></title></Head><Body><Scripttype= "Text/javascript">window.onload= function() {persentvalue (); } functionAddvaluetocookie () {Setcookie ("ID", -); Setcookie ("Name", 'Tom'); Persentvalue (); } functionClearvalueofcookie () {Setcookietoexpire ("ID"); Setcookietoexpire ("Name"); Persentvalue (); } functionPersentvalue () {varID=GetCookie ("ID"); varname=GetCookie ("Name"); if(ID== NULL) {alert ('waiting for Set value to ID'); } Else{alert (ID); } if(Name== NULL) {alert ('waiting for Set value to Name'); } Else{alert (name); } } functionSetcookie (name, value) {var Days= -; varExp= NewDate (); Exp.settime (Exp.gettime ()+ Days* - * - * - * +); Document.cookie=name+ "=" +Escape (value)+ "; expires=" +exp.togmtstring (); } functionGetCookie (name) {varArg=name+ "="; varAlen=arg.length; varClen=document.cookie.length; varI= 0; while(i<Clen) { varJ=I+Alen; if(Document.cookie.substring (i, J)==Arg) { returnGetcookieval (j); } I=Document.cookie.indexOf (" ", i)+ 1; if(i== 0) Break; } return NULL; } functionGetcookieval (offset) {varEndstr=Document.cookie.indexOf (";", offset); if(Endstr== -1) {Endstr=document.cookie.length; } returnunescape (document.cookie.substring (offset, endstr)); } functionSetcookietoexpire (name) {varExp= NewDate (); Exp.settime (Exp.gettime ()- 1); varCval=GetCookie (name); if(Cval!= NULL) Document.cookie=name+ "=" +Cval+ "; expires=" +exp.togmtstring (); }</Script><inputtype= "button"value= "AddValue"onclick= "Addvaluetocookie ();" /><inputtype= "button"value= "ClearValue"onclick= "Clearvalueofcookie ();" /></Body></HTML>
View Code
Reference links
Usage of cookies in JavaScript