1,jquery.cookie.js
這篇文章已經寫的很詳細了:
http://www.cnblogs.com/afuge/archive/2013/07/03/3169048.html
2,原生js操作cookie
jquery.cookie.js一般會出現cookie不兼容問題就要用原生的js了
//獲得coolie 的值
function cookie(name) {
var cookieArray=document.cookie.split("; "); //得到分割的cookie名值對
var ;i++){
var arr=cookieArray[i].split("="); //將名和值分開
if(arr[0]==name)return unescape(arr[1]); //如果是指定的cookie,則傳回它的值
}
return "";
}
document.cookie = name+"=;expires ="+(new Date(0)).toGMTString();} function getCookie(objName){//取得指定名稱的cookie的值 . "); for(var i = 0;i var temp = arrhard objName) return unescape(temp[1]); }} function addCookie(objName,objValue,objHours* jName + "=" + escape(objValue) ; if(objHours > 0){ var date = new Date(); var ms = objHours*3600*1000; date.setTime(date.getTime() + ms); str += "; expires=" + date.toGMTString();ookie.
function SetCookie (name,value)//兩個參數,一個是cookie的名子,一個是值
{
var Days = 30; //此cookie 將保留30 天
//new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
value) + ";expires=" + exp.toGMTString();} function getCookie(name)//取cookies (new RegExp(" (^| )"+name+"=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return null;
function delCookie(name)//刪除cookie
{
var exp = new Date();
Cookie(name); if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();}端處理
///
/// 將已點擊的值存放至Cookie中
///
string keyName)
{
HttpCookie cookie = new HttpCookie(cookieName);//初使化並設定Cookie的名稱
if (context.Request.Cookies[cookieName] != null)
ookie = context.Request.Cookies[cookieName];
if (cookie. Values.Count > 0 && cookie.Values.GetValues(keyName) != null && cookie.Values.GetValues(keyName).Length > 0) Remove(cookieName, keyName);
}
TimeSpan(0, 3, 0, 0, 0);//過期時間為3小時
cookie.Expires = DateTime.Now.Add(ts) ;
context.Response.AppendCookie(cookie);
//NCookieUtil.SetCookie(keyName, dic); /// 將點選的數值從Cookie中取出
/// summary>
public string GetClickValue(HttpContext context, string cookieName, string keyName)
{Name if (context.Request.Cookies[cookieName] != null)
{
[cookieName][keyName] != null)
{
}
}
//userName ;
}
///
//// 移除cookie中指定的鍵,若是最後一個鍵則移除此 void Remove(string cookieName, string keyName )
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName {
if (cookie.Values.Count > if Values.Count = = 1)
{
cookie.Values.Remove(HttpUtility.UrlEncode(keyName));
cookie.Expires = DateTime.Now.AddDays(-1);
}
{
cookie.Values.Remove(Http }
HttpContext.Current.Response.Cookies.Add(cookie) ;
}
}
}
其實這三種方法或建議使用第二種方法!
以上就是cookie的三種操作方法的內容,更多相關文章請關注PHP中文網(www.php.cn)!