Interaction between JavaScript and cookies

Source: Internet
Author: User

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>

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.