js读取cookie字典

来源:互联网 发布:淘宝达人有用吗 编辑:程序博客网 时间:2024/05/29 16:50
前面写到过asp读取cookies字典的
现在写一个js读取cookies的字典的

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="zourinet">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>
<script language="JavaScript">
<!--
function Cookies()
{
    this.name;
    this.path="/";
    expires=new Date();
    expires.setTime(expires.getTime()+(86400*365));
    this.expires = expires.toGMTString();
}
Cookies.prototype.SetCookie=function (name,value)
{
    document.cookie=name+"="+value+"; expires="+this.expires+"; path="+this.path+"";
}
Cookies.prototype.getCookie=function (Name)
{
   var search = Name + "=";
   if (document.cookie.length > 0)
   { // if there are any cookies
      offset = document.cookie.indexOf(search)
      if (offset != -1)
      { // if cookie exists
         offset += search.length ;
         end = document.cookie.indexOf(";", offset)
         if (end == -1)
            end = document.cookie.length;       
         return (unescape(document.cookie.substring(offset, end)));
      }
   }
   return 0;
}

//未完等等[20070825]
Cookies.prototype.getCookieDic=function(vparent,vchildren)
{
    var strchildren =  this.getCookie(vparent);   
    strchildren = strchildren.getQuery(vchildren);
    return (strchildren);////20070827 完成
}
String.prototype.getQuery = function(name)
{
  var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  var r = this.substr(this.indexOf("/?")+1).match(reg);
  if (r!=null) return unescape(r[2]); return null;
}
var c = new Cookies();
c.SetCookie("person","name=zourinet&id=22");
alert(c.getCookieDic("person","id"));
//-->
</script>
</body>
</html>

完成.