JavaScript设置Cookie、获取Cookie和清除Cookie

function getCookieValue(objName){
  //获取指定名称的Cookie的值
  var arrayCookie = document.cookie.split(";");
  for(var i = 0;i < arrayCookie.length;i ++){
    var temp = arrayCookie[i].split("=");
    if(temp[0].indexOf(objName)>=0){
      return unescape(temp[1]);
    }
  }
}

function setCookie(objName,objValue,objHours){
    //设置Cookie
    var str = objName + "=" + escape(objValue);
    if(objHours > 0){
      var date = new Date();
      var ms = objHours*3600*1000;
      date.setTime(date.getTime() + ms);
      str += "; expires=" + date.toGMTString();
    }
   document.cookie = str;
}

function clearCookie(objName){

    var date = new Date();
    date.setTime (date.getTime() - 1);
    var objValue = getCookieValue (objName);
    document.cookie = objName + "=" + objValue + "; expires="+ date.toGMTString();
}

JavaScript设置Cookie、获取Cookie和清除Cookie,布布扣,bubuko.com

JavaScript设置Cookie、获取Cookie和清除Cookie

上一篇:java中String常量的存储原理


下一篇:javascript,从库到框架再到平台