Javaphua Blog

BlogJava 首页 新随笔 联系 聚合 管理
  46 Posts :: 5 Stories :: 46 Comments :: 0 Trackbacks
关于javascript对cookie的设置与读取

<script>
//设置Cookie
  function setCookie(va){
       var expires = new Date();
      expires.setTime(expires.getTime() + 12 * 30 * 24 * 60 * 60 * 1000);
      /*   一年 x 一个月当作 30 天 x 一天 24 小时
      x 一小时 60 分 x 一分 60 秒 x 一秒 1000 毫秒 */
       document.cookie=va+';expires='+expires.toGMTString();
  }
  //读取Cookie
  function readCookie(name){
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)  {
    offset = document.cookie.indexOf(search);
    if (offset != -1)    {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

setCookie("user=123");
alert(readCookie('user'));
</script>
posted on 2006-11-11 19:58 Javaphua 阅读(324) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: