用js来设置cookie有什么好处呢?在前端进行操作时,有的时候要传很多参数,这个时候,我们可以把这些参数拼接到url后面进行传值,那边在接收参数,不过这样挺麻烦的,如果把这些数据放到cookie里面,就开发者的开发效率来说,我想会高一些,以淘宝为例,他有一级分类,二级分类,三级,四级,我们找商品的时候,可能会牵扯到很多条件,如果把这些条件都放到cookie里面,就方便多了。
- JS对Cookie的读取
//取得cookie function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); //把cookie分割成组 for(var i=0;i < ca.length;i++) { var c = ca[i]; //取得字符串 while (c.charAt(0)==' ') { //判断一下字符串有没有前导空格 c = c.substring(1,c.length); //有的话,从第二位开始取 } if (c.indexOf(nameEQ) == 0) { //如果含有我们要的name return unescape(c.substring(nameEQ.length,c.length)); //解码并截取我们要值 } } return false; } //清除cookie function clearCookie(name) { setCookie(name, "", -1); } //设置cookie function setCookie(name, value, seconds) { seconds = seconds || 0; //seconds有值就直接赋值,没有为0,这个根php不一样。 var expires = ""; if (seconds != 0 ) { //设置cookie生存时间 var date = new Date(); date.setTime(date.getTime()+(seconds*1000)); expires = "; expires="+date.toGMTString(); } document.cookie = name+"="+escape(value)+expires+"; path=/"; //转码并赋值 }
调用一下上面方法: setCookie("test","tank",1800); //设置cookie的值,生存时间半个小时 alert(getCookie('test')); //取得cookie的值,显示tank clearCookie("test"); //删除cookie的值 alert(getCookie('test')); //test对应的cookie值为空,显示为false.就是getCookie最后返的false值。
- C#对Cookie的读取
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public class Cookie { /// <summary> /// Cookies赋值 /// </summary> /// <param name="strName">主键</param> /// <param name="strValue">键值</param> /// <param name="strDay">有效天数</param> /// <returns></returns> public bool setCookie(string strName, string strValue, int strDay) { try { HttpCookie Cookie = new HttpCookie(strName); //Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com Cookie.Expires = DateTime.Now.AddDays(strDay); Cookie.Value = strValue; System.Web.HttpContext.Current.Response.Cookies.Add(Cookie); return true; } catch { return false; } } /// <summary> /// 读取Cookies /// </summary> /// <param name="strName">主键</param> /// <returns></returns> public string getCookie(string strName) { HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName]; if (Cookie != null) { return Cookie.Value.ToString(); } else { return null; } } /// <summary> /// 删除Cookies /// </summary> /// <param name="strName">主键</param> /// <returns></returns> public bool delCookie(string strName) { try { HttpCookie Cookie = new HttpCookie(strName); //Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com Cookie.Expires = DateTime.Now.AddDays(-1); System.Web.HttpContext.Current.Response.Cookies.Add(Cookie); return true; } catch { return false; } } }
- 示例
Cookie Cookie = new Cookie(); Cookie.setCookie("name", "aaa",1);//赋值 Cookie.getCookie("name");//取值 Cookie.delCookie("name");//删除 注意:当Cookie存中文出现乱码,则在存放时给中文编码,如Cookie.setCookie("name", Server.UrlEncode("aaa"),1),读取时解码即可 另外:只要不给cookie设置过期时间,cookie在浏览器关闭的时候自动失效