首页 > 网站 > WEB开发 > 正文

cookie的三种操作方法

2024-04-27 15:01:40
字体:
来源:转载
供稿:网友

1,jquery.cookie.js

这一篇文章已经写的很详细了:

http://www.cnblogs.com/afuge/archive/2013/07/03/3169048.html

2,原生js操作cookie

jquery.cookie.js一般会出现cookie不兼容问题就要用原生的js了

<script language=javascript>
 
//获得coolie 的值
function cookie(name){   
  var cookieArray=document.cookie.split("; "); //得到分割的cookie名值对   
   var cookie=new Object();   
   for (var i=0;i<cookieArray.length;i++){   
      var arr=cookieArray[i].split("=");       //将名和值分开   
      if(arr[0]==name)return unescape(arr[1]); //如果是指定的cookie,则返回它的值   
    
   return "";
}
 
function delCookie(name)//删除cookie
{
   document.cookie = name+"=;expires="+(new Date(0)).toGMTString();
}
 
function getCookie(objName){//获取指定名称的cookie的值
    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){      //添加cookie
    var str = objName + "=" + escape(objValue);
    if(objHours > 0){                               //为时不设定过期时间,浏览器关闭时cookie自动消失
        var date = new Date();
        var ms = objHours*3600*1000;
        date.setTime(date.getTime() + ms);
        str += "; expires=" + date.toGMTString();
   }
   document.cookie = str;
}
 
function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
{ 
    var Days = 30; //此 cookie 将被保存 30 天
    var exp = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
 
function getCookie(name)//取cookies函数       
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;
}
 
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
</script>

3,服务器端处理

 /// <summary>
    /// 将点击的值存放到Cookie中
    /// </summary>
    PRivate void SetCookieValue(HttpContext context, string dic, string cookieName, string keyName)
    {
        HttpCookie cookie = new HttpCookie(cookieName);//初使化并设置Cookie的名称
        if (context.Request.Cookies[cookieName] != null)
        {
            cookie = context.Request.Cookies[cookieName];
            if (cookie.Values.Count > 0 && cookie.Values.GetValues(keyName) != null && cookie.Values.GetValues(keyName).Length > 0)
            {
                Remove(cookieName, keyName);
            }
        }
        TimeSpan ts = new TimeSpan(0, 3, 0, 0, 0);//过期时间为3小时
        cookie.Expires = DateTime.Now.Add(ts);//设置过期时间
        cookie.Values.Add(keyName, dic);
        context.Response.AppendCookie(cookie);
        //NCookieUtil.SetCookie(keyName, dic);
    }
    /// <summary>
    /// 将点击的值从Cookie中取出
    /// </summary>
    public string GetClickValue(HttpContext context, string cookieName, string keyName)
    {
        string userName = "";
        if (context.Request.Cookies[cookieName] != null)
        {
            if (context.Request.Cookies[cookieName][keyName] != null)
            {
                userName = context.Request.Cookies[cookieName][keyName].ToString();
            }
        }
        //userName = NCookieUtil.GetCookie(keyName);
        return userName;
    }
    /// <summary>  
    /// 移除cookie中指定的键,若是最后一个键则移除这个cookie  
    /// </summary>  
    public static void Remove(string cookieName, string keyName)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
        if (cookie != null)
        {
            if (cookie.Values.Count > 0)
            {
                if (cookie.Values.Count == 1)
                {
                    //若是最后一个键则移除这个cookie,否则会多出一个空值  
                    cookie.Values.Remove(HttpUtility.UrlEncode(keyName));
                    cookie.Expires = DateTime.Now.AddDays(-1);
                }
                else
                {
                    cookie.Values.Remove(HttpUtility.UrlEncode(keyName));
                }
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }

其实这三种方法还是建议用第二种方法!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表