JS实现Cookie读、写、删除操作工具类示例
程序员文章站
2022-06-19 23:18:54
本文实例讲述了js实现cookie读、写、删除操作工具类。分享给大家供大家参考,具体如下:
/***
*读取指定的cookie值 readcookie("id"...
本文实例讲述了js实现cookie读、写、删除操作工具类。分享给大家供大家参考,具体如下:
/*** *读取指定的cookie值 readcookie("id"); *@param {string} cookiename cookie名称 */ function readcookie(cookiename) { var thecookie = "" + document.cookie; var ind = thecookie.indexof(cookiename); if(ind==-1 || cookiename=="") return ""; var ind1 = thecookie.indexof(';',ind); if(ind1==-1) ind1 = thecookie.length; /*读取cookie值*/ return unescape(thecookie.substring(ind+cookiename.length+1,ind1)); } /*** * 设置cookie值 setcookie("id",1); * @param {string} cookiename cookie名称 * @param {string} cookievalue cookie值 * @param {number} ndays cookie过期天数 */ function setcookie(cookiename, cookievalue) { /*当前日期*/ var today = new date(); /*cookie过期时间*/ var expire = new date(); /*如果未设置ndays参数或者ndays为0,取默认值1*/ //if(ndays == null || ndays == 0) ndays = 1; /*计算cookie过期时间【 3600000 * 24 为一天】*/ expire.settime(today.gettime() + 400000); //5分钟 document.cookie = cookiename + "=" + escape(cookievalue) + ";expires=" + expire.togmtstring(); } /*** * 删除cookie中指定变量函数 * @param {string} $name cookie名称 */ function deletecookie($name){ var mydate=new date(); mydate.settime(-1000);//设置时间 document.cookie=$name+"=''; expires="+mydate.togmtstring(); } /*** * 删除cookie中所有定变量函数 * @param {string} cookiename cookie名称 * @param {string} cookievalue cookie值 * @param {number} ndays cookie过期天数 */ function clearcookie(){ var mydate=new date(); mydate.settime(-1000);//设置时间 var data=document.cookie; var dataarray=data.split("; "); for(var i=0;i<dataarray.length;i++){ var varname=dataarray[i].split("="); document.cookie=varname[0]+"=''; expires="+mydate.togmtstring(); } }
附:js操作cookie的小插件
var cookieutil = { // 设置cookie set : function (name, value, expires, domain, path, secure) { var cookietext = ""; cookietext += encodeuricomponent(name) + "=" + encodeuricomponent(value); if (expires instanceof date) { cookietext += "; expires=" + expires.togmtstring(); } if (path) { cookietext += "; path=" + path; } if (domain) { cookietext += "; domain=" + domain; } if (secure) { cookietext += "; secure"; } document.cookie = cookietext; }, // name=value; expires=expiration_time; path=domain_path; domain=domain_name; secure // 获取cookie get : function (name) { var cookiename = encodeuricomponent(name) + "=", cookiestart = document.cookie.indexof(cookiename), cookievalue = ""; if (cookiestart > -1) { var cookieend = document.cookie.indexof (";", cookiestart); if (cookieend == -1) { cookieend = document.cookie.length; } cookievalue = decodeuricomponent(document.cookie.substring(cookiestart + cookiename.length, cookieend)); } return cookievalue; }, // 删除cookie unset : function (name, domain, path, secure) { this.set(name, "", date(0), domain, path, secure); } };
更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript数据结构与算法技巧总结》、《javascript遍历算法与技巧总结》、《javascript查找算法技巧总结》、《javascript动画特效与技巧汇总》、《javascript错误与调试技巧总结》及《javascript数学运算用法总结》
希望本文所述对大家javascript程序设计有所帮助。
下一篇: 曹操为什么会挟天子以令诸侯?真相是什么