I need some help creating the necessary Javascript to add values to an existing cookie. I know how to do it in c#, but it needs to be done in Javascript in this scenario.
This is the C# Code:
HttpCookie myCookie = HttpContext.Current.Request.Cookies["SiteSettings"];
myCookie.Values.Add(varName, varValue);
myCookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(myCookie);
Can anyone help me convert this to Javascript? I’ve searched high and low on the internet, and most tutorials don’t have a way of specifying the cookie (this site has more than one cookie).
Thank you very much,
Andrew
Not sure where you’ve looked, since the first hit on google for “javascript cookies” is this excellent article by ppk: http://www.quirksmode.org/js/cookies.html
It should answer your question and explain other JS-related cookie-nuances, including example functions for handling cookies in a more sensible way than string concatenation.