Home > Article > Web Front-end > Three ways to operate cookies
1, jquery.cookie.js
This article has been written in great detail:
http://www.cnblogs.com/afuge/archive/2013/07/03/3169048.html
2, When using native js to operate cookies
jquery.cookie.js, there will usually be cookie incompatibility issues, so you need to use native js
//Get the value of coolie
function cookie(name ){
var cookieArray=document.cookie.split("; "); //Get the split cookie name-value pair
var cookie=new Object();
for (var i=0;i var arr=cookieArray[i].split("="); //Separate the name and value if(arr[0]==name)return unescape(arr[1]); / /If it is the specified cookie, return its value } return ""; } function delCookie(name)//Delete cookie { document.cookie = name+"=; expires="+(new Date(0)).toGMTString(); } function getCookie(objName){//Get the value of the cookie with the specified name var arrStr = document.cookie.split(" ; "); for(var i = 0;i < arrStr.length;i ++){ var temp = arrStr[i].split("="); if(temp[0] == objName) return unescape(temp[1]); } } function addCookie(objName,objValue,objHours){ //Add cookie var str = objName + "=" + escape( Objvalue); if (Objhours & GT; 0) {// does not set the expiration time when the browser is closed. ;. Date.Settime (date.gettime () + ms); str + = "; expires =" + date.togmtring ();
///
/// Store the click value in the cookie
///
PRivate void SetCookieValue(HttpContext context, string dic, string cookieName, string keyName)
{
HttpCookie Cookie = New HttpCookie (cookiename); // The name of the cookies and set the name of the cookie
if (context.request.cookies [cookiename]! = Null) {
cookie = context.request.coo okies [cookiename];
IF ( cookie.Values.Count > 0 && cookie.Values.GetValues(keyName) != null && cookie.Values.GetValues(keyName).Length > 0)
}
TimeSpan ts = new TimeSpan(0, 3, 0, 0, 0);//The expiration time is 3 hours
cookie.Expires = DateTime.Now.Add(ts);//Set the expiration time
cookie.Values.Add( keyName, dic);
context.Response.AppendCookie(cookie);
//NCookieUtil.SetCookie(keyName, dic);
}
///
/// Get the clicked value out of the cookie
///
public string GetClickValue(HttpContext context, string cookieName, string keyName)
{
string userName = "";
if (context.Request.Cookies[cookieName] != null)
(context.Request.Cookies[cookieName][keyName] != null)
}
//userName = NCookieUtil.GetCookie( keyName);
return userName; void Remove(string cookieName, string keyName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie != null)
{
if (cookie.Values.Count > 0)
Cookie.Values.Remove(HttpUtility.UrlEncode(keyName));
cookie.Expires = DateTime.Now.AddDays (-1); HttpUtility.UrlEncode(keyName));
HttpUtility. ;
}
}}}
In fact, these three methods are still recommended to use the second method!
The above is the content of the three operating methods of cookies. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!