I'm writing HTML/CSS/JavaScript code and want a function or solution so that when the user refreshes the page the theme stays the same.
Preferably a simple solution because I am a beginner in this field.
Thank you.
I do not know how you are applying the theme but what you could do is to store the current theme in the localstorage and read it when the page has loaded.
window.addEventListener("DOMContentLoaded", () => {
let theme = localStorage.getItem("theme");
if (theme === "dark") {
// insert how you apply your theme
} else {
// apply other theme
}
});
To store a theme you would then want to use the setItem method.
localStorage.setItem("theme", "your value goes here")
Hope this is what you were looking for.