WEB开发网
开发学院网页设计JavaScript 在客户端设置cooke和获取cooke的函数 阅读

在客户端设置cooke和获取cooke的函数

 2005-11-06 19:53:42 来源:WEB开发网   
核心提示:<script language="javascript">function setCookie(name, value, expires, path, domain, secure) { var today = new Date(); var expiry = new Date(t

<script language="javascript">
function setCookie(name, value, expires, path, domain, secure)
{
  var today = new Date();
  var expiry = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000);
  if(expires==''||expires==null)
  {
 expires=expiry;
  }
 
  var curCookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
  document.cookie = curCookie;
 
}

function getCookie(name)
{
  var dc = document.cookie;
  var PRefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1)
  {
   begin = dc.indexOf(prefix);
   if (begin != 0) return null;
  } else
   begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
   end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

</script>

Tags:客户端 设置 cooke

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接