javascript - Cookie not deleting or changing
问题描述
I have been working on a site that instructs my peers on how to use many of the customization features and software on their provided Windows 10 devices.
I am trying to add a dark theme to the site, and most of the work is there, but I am having trouble with using cookies to allow the theme selection persist throughout the website and multiple sessions. My issue is that visiting the webpage adds a cookie to enable the dark theme, but there should be no cookie initially.
My intent is for the website to default to the light theme when there is no cookie for the dark theme. In addition, if there is a cookie for the dark theme, when the user clicks the button for the light theme, the cookie for the dark theme should be deleted.
Through the process of making this website, I have been learning a lot, but I have been stumped by this issue. What am I doing wrong that causes the cookie to be stuck as theme=dark
?
Script to restore the theme on page load:
function restoreTheme(){
if(document.cookie="theme=dark") {
darkTheme();
} else {
lightTheme();
}
}
Script for changing theme:
function darkTheme() {
document.body.style.setProperty("--sideNavBackground", "rgb(33,33,33)");
document.body.style.setProperty("--bodyBackgroundColor", "rgb(17,17,17)");
document.body.style.setProperty("--sideNavItemBackgroundHover", "rgb(64,64,64)");
document.body.style.setProperty("--bodyTextColor", "rgb(238,238,238)");
document.cookie="theme=dark";
document.cookie = "theme=light; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}
function lightTheme() {
document.body.style.setProperty("--sideNavBackground", "rgb(230,230,230)");
document.body.style.setProperty("--bodyBackgroundColor", "rgb(255,255,255)");
document.body.style.setProperty("--sideNavItemBackgroundHover", "rgb(199,199,199)");
document.body.style.setProperty("--bodyTextColor", "rgb(17,17,17)");
document.cookie="theme=light";
document.cookie = "theme=dark; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}
Buttons to change theme:
<form>
<input name="theme" type="submit" class="theme_b" id="dark" onclick="darkTheme()" value="Dark Theme">
<input name="theme" type="submit" class="theme_b" id="light" onclick="lightTheme()" value="Light Theme">
</form>
For reference, here is a codepen of the (mostly) complete page: https://codepen.io/jackemery2001/pen/LrPvEJ/
解决方案
使用@Erich Brahmin 和@CBroe 的建议,我能够解决这个问题。
我function restoreTheme()
改为
function restoreTheme() {
var theme = getCookie("theme");
if (theme=="dark") {
//alert("dark");
darkTheme();
} else {
//alert("light");
lightTheme();
}
}
并在 getCookie() 的头部添加了一个脚本:
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
简单地将 document.cookie="theme=dark" 更改为 document.cookie=="theme=dark" 并不完全有效,因为它只有在没有其他 cookie 时才有效。
function restoreTheme(){
if(document.cookie="theme=dark") {
darkTheme();
} else {
lightTheme();
}
推荐阅读
- javascript - 在javascript中将列表中的单词与句子中的单词匹配的最佳方法是什么?
- google-apps-script - 使用 Apps 脚本在 Google 表格中将非连续单元格从一列复制到另一列
- react-admin - DateInput 为 allowEmpty 输入生成空字符串
- python - 错误:permission_manager_qt.cpp(82) 不支持的权限类型:13
- azure - Azure Web App 是否支持配置文件中的字符串/令牌替换?
- python - 如何将像素值替换为张量流操作?
- javascript - 如何在fullCalendar中禁用事件跨度到另一天?
- python-3.x - 如何使用其方言查找所有出现的阿拉伯字符?
- ios - 如何避免在 IOS 中快速显示动作“剪切、复制、查找……”
- excel - MIN 函数不忽略 FALSE