JavaScript数据结构与算法技巧总结

正文实例讲述了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程序设计有着支持。

您只怕感兴趣的稿子:

  • js验证手提式有线电话机号、密码、短信验证码代码工具类
  • javaScript日期工具类DateUtils详解
  • js提取汉语拼音首字母的卷入工具类
  • 详解javascript常用工具类的包装
  • javaScript字符串工具类StringUtils详解
  • Jsp
    servlet验证码工具类分享
  • Java达成的JSONUtil工具类与用法示例
  • javaScript手提式有线电电话机号码校验工具类PhoneUtils详解
  • JavaScript日期工具类DateUtils定义与用法示例
  • JavaScript封装的常用工具类库bee.js用法详解【杰出类库】

相关文章