JavaScript中无法删除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
相关产品推荐
相关产品推荐

