一个操作Cookies的实例

博客展示了使用JavaScript操作cookie的代码。包含获取cookie值、设置cookie、获取指定名称cookie等函数,还涉及表单输入值检查,若为空给出提示,检查通过则设置用户ID的cookie。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

操作cookie

---------------------------------------------------------------------------------------


function getCookieVal (offset)
 {
 var endstr = documents.cookie.indexOf (";", offset);
 if (endstr == -1)
  endstr = documents.cookie.length;
 return unescape(documents.cookie.substring(offset, endstr));
 }
function SetCookie (name, value)
 {
 var argv = SetCookie.arguments;
 var argc = SetCookie.arguments.length;
 var expires = (2 < argc) ? argv[2] : null;
 expires = new Date("December 31, 2023");
 var path = (3 < argc) ? argv[3] : null;
 var domain = (4 < argc) ? argv[4] : null;
 var secure = (5 < argc) ? argv[5] : false;
 documents.cookie = name + "=" + escape (value) +
  ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  ((path == null) ? "" : ("; path=" + path)) +
  ((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
 }

function GetCookie (name)
 {
 var arg = name + "=";
 var alen = arg.length;
 var clen = documents.cookie.length;
 var i = 0;
 while (i < clen)
  {
  var j = i + alen;
  if (documents.cookie.substring(i, j) == arg)
    return getCookieVal (j);
  i = documents.cookie.indexOf(" ", i) + 1;
  if (i == 0)
    break;
  }
 return null;
 }

function check_value()
 {
   uid=eval("window.document.myform.elements['fields[uid]']");
   if (uid.value.Trim()=='')
    {
      alert("抱歉,用戶名不能為空!");
      uid.focus();
      return false;
    }
   pws=eval("window.document.myform.elements['fields[pws]']");
   if (pws.value.Trim()=='')
    {
      alert("抱歉,密碼不能為空!");
      pws.focus();
      return false;
    }

   tz=eval("window.document.myform.elements['fields[tz]']");
   if (tz.value.Trim()=='')
    {
      alert("抱歉,請選擇要登錄的套帳!");
      uid.focus();
      return false;
    }
   SetCookie ("uid",uid.value );
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值