You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript中无法删除Cookie问题求助

无法删除名为CalcHistory的Cookie问题

我自行编写了一套用于设置、获取和加载Cookie的函数,目前用这些函数保存表单注册信息(包含用户名、邮箱、年龄、生日)功能正常,可完成设置、读取并加载到指定区域。但后续尝试用Cookie存储其他数据时出现问题:现在我想要删除名为CalcHistory的Cookie,虽然能通过GetCookie函数获取到该Cookie,却无法用DeleteCookie函数将其删除。我已经查阅过StackOverflow相关话题并咨询过ChatGPT,但问题仍未解决。

以下是相关代码:

// DeleteCookie function witch gets its value from an input but i aslo tried handing value manually 
// and it did not work
function DeleteCookie(cookieName) {
  document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; SameSite=Strict";
  console.log(document.cookie)
}

// SetCookie Function
function SetCookie(name, value, expire) {
  let Name = name.toString();
  let Value = value.toString();
  var currentDate = new Date().getTime();
  var ExpirationDate = new Date(currentDate + (expire * 24 * 60 * 60 * 1000)).toString();
  document.cookie = Name + "=" + Value + ";" + "expires=" + ExpirationDate +  ";" + "path=/" + ";" + "SameSite=Strict" + ";";
}

// GetCookie Function
function GetCookie(CookieName){
  let Name = CookieName + "=";
  let CookieArray = document.cookie.split(";");
  for(n in CookieArray){
    let Cookies = CookieArray[n].trim();
    if (Cookies.indexOf(Name) == 0){
      return Cookies.substring(Name.length);
    }
  }  
  return null;
}

//Self Invoking function so my account info data loads up on page reload 
(function CookieLoader(){
  let UserName = GetCookie("UserName");
  let UserAge = GetCookie("UserAge");
  let UserEmail = GetCookie("UserEmail");
  let UserBirthDate = GetCookie("UserBirthDate");
  document.getElementById("account-info").innerHTML = 
  "Username : " + UserName + "<br>" +
  "Age : " + UserAge + "<br>" +
  "Email : " + UserEmail + "<br>" +
  "BirthDay : " + UserBirthDate + "<br>" ;
})()

内容的提问来源于stack exchange,提问作者Resident Evil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 08:01:09