• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
内蒙古峰回路转armyfeng
博客园    首页    新随笔    联系   管理    订阅  订阅

在JAVASCRIPT中使用COOKIE

                                    
  读取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;
  }
  alert( readCookie(
"myCookie") );
  
  写入cookie:
  
function writeCookie(name, value, hours)
  {
  
var expire = "";
  
if(hours != null)
   {
   expire
= new Date((new Date()).getTime() + hours * 3600000);
   expire
= "; expires=" + expire.toGMTString();
   }
   document.cookie
= name + "=" + escape(value) + expire;
  }
  writeCookie(
"myCookie", "my name", 24);
  
  
---------------------------------------------------------------
  
  
+ 号字符串连接符号
  
  至于cookies,请看:
  
  用 COOKIES 记录一个人访问次数的小程序。
  
  
<script language="JavaScript">
  
<!--
  
var caution = false
  
function setCookie(name, value, expires, path, domain, secure) {
  
var curCookie = name + "=" + escape(value) +
   ((expires)
? "; expires=" + expires.toGMTString() : "") +
   ((path)
? "; path=" + path : "") +
   ((domain)
? "; domain=" + domain : "") +
   ((secure)
? "; secure" : "")
  
if (!caution | | (name + "=" + escape(value)).length <= 4000)
   document.cookie
= curCookie
  
else
  
if (confirm("Cookie exceeds 4KB and will be cut!"))
   document.cookie
= curCookie
  }
  
function getCookie(name) {
  
var prefix = name + "="
  
var cookieStartIndex = document.cookie.indexOf(prefix)
  
if (cookieStartIndex == -1)
  
return null
  
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex +
  prefix.length)
  
if (cookieEndIndex == -1)
   cookieEndIndex
= document.cookie.length
  
return unescape(document.cookie.substring(cookieStartIndex +
  prefix.length, cookieEndIndex))
  }
  
function deleteCookie(name, path, domain) {
  
if (getCookie(name)) {
   document.cookie
= name + "=" +
   ((path)
? "; path=" + path : "") +
   ((domain)
? "; domain=" + domain : "") +
  
"; expires=Thu, 01-Jan-70 00:00:01 GMT"
   }
  }
  
function fixDate(date) {
  
var base = new Date(0)
  
var skew = base.getTime()
  
if (skew > 0)
   date.setTime(date.getTime()
- skew)
  }
  
var now = new Date()
  fixDate(now)
  now.setTime(now.getTime()
+ 365 * 24 * 60 * 60 * 1000)
  
var visits = getCookie("counter")
  
if (!visits)
   visits
= 1
  
else
   visits
= parseInt(visits) + 1
  setCookie(
"counter", visits, now)
  document.write(
"欢迎您第 " + visits + " 次来到本站点")
  
// -->
  </script>
  
  
---------------------------------------------------------------
  
  
function setCookie(name,value)
  {
  
var Days = 30;
  
var exp = new Date(); //new Date("December 31, 9998");
   exp.setTime(exp.getTime() + Days*24*60*60*1000);
   document.cookie
= name + "="+ escape (value) + ";expires=" + exp.toGMTString();
  }
  
function getCookie(name)
  {
  
var arr,reg=new RegExp("(^ | )"+name+"=([^;]*)(; |$)");
  
if(arr=document.cookie.match(reg)) return unescape(arr[2]);
  
else return null;
  }
  
function delCookie(name)
  {
  
var exp = new Date();
   exp.setTime(exp.getTime()
- 1);
  
var cval=getCookie(name);
  
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
  }
  
---------------------------------------------------------------
  
  http:
//www.hkln.net/js/advance/cookie.htm
  ---------------------------------------------------------------
  
  读写cookie的时候最好用escape 和unescape
  
---------------------------------------------------------------
  
  浏览器地址栏打:
  
  javascript:alert(document.cookie);
posted @ 2006-09-24 14:21  老冯  阅读(204)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3