Javascript读写Cookies

| |
[不指定 2006/10/29 23:33 | by ipaddr ]
[code]
<script type="text/javascript">
// 写 cookie
function setCookie(sName, sValue, iTime)
{
 var date = new Date();
 date.setTime(date.getTime()+iTime*1000);
 document.cookie = escape(sName) + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}

// 读 cookie
function getCookie(sName)
{
 var aCookie = document.cookie.split("; ");
 for (var i=0; i < aCookie.length; i++)
 {
   var aCrumb = aCookie[i].split("=");
   if (escape(sName) == aCrumb[0])
     return unescape(aCrumb[1]);
 }
 return null;
}

// 删 cookie
function delCookie(sName){
 var date = new Date();
 document.cookie = sName + "= ; expires=" + date.toGMTString();
}

// 测试函数
function trace(){
 alert(document.cookie);
}
</script>
[/code]
Program | 评论(0) | 引用(15) | 阅读(4224)