In web, I used ajax to interact with the server, without considering how javascript can store data on the client. Recently, when working on a prototype, I found that it is still very troublesome for JavaScript to save data on the client. For example, in a level-2 menu on the left of the page, how can I keep the selected status while refreshing the page.
Interaction between JavaScript and cookies:
<Script language = JavaScript>
Function set_cookie (name, value ){
VaR argv = set_cookie.arguments;
VaR argc = set_cookie.arguments.length;
VaR expires = new date ();
VaR Path = (argc> 3 )? Argv [3]: NULL;
VaR domain = (argc> 4 )? Argv [4]: NULL;
Expires. setyear (4002 );
Expires. setmonth (10 );
Expires. setdate (1 );
Expires. sethours (2 );
Expires. setminutes (3 );
St = Name + "=" + escape (value) + "; expires =" + expires. togmtstring () + (Path = NULL )? "": ("; Path =" + path) + (domain = NULL )? "": ("; Domain =" + domain ));
Document. Cookie = sT;
}
Function get_cookie (name ){
VaR Arg = Name + "= ";
VaR Alen = Arg. length;
VaR clen = Document. Cookie. length;
VaR I = 0;
While (I <clen ){
VaR J = I + Alen;
If (document. Cookie. substring (I, j) = Arg ){
Offset = J;
VaR endstr = Document. Cookie. indexof (";", offset );
If (endstr =-1)
Endstr = Document. Cookie. length;
Return Unescape (document. Cookie. substring (offset, endstr ));
}
I = Document. Cookie. indexof ("", I) + 1;
If (I = 0)
Break;
}
Return NULL;
}
Function del_cookie (name ){
VaR expdate = new date ();
Expdate. settime (expdate. gettime ()-1 );
Set_cookie (name, "", expdate)
}
</SCRIPT>