Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Answered Help with cookies that will save site settings

NotRankovic

New Coder
Hello,
I'm new to learning java script, I made a small script that will change the theme on my site (and icon for dark theme and light theme), but I have the following problem:
I don't know how to create cookies that when someone turns on the dark theme, they save it, and when someone refreshes the site, it loads the site in the theme the user was in last time.
Thanks!

JavaScript:
    <script>

        var icon = document.getElementById("icon")

        icon.onclick = function() {
            document.body.classList.toggle("dark-mode");
            if(document.body.classList.contains("dark-mode")){
                icon.src = "img/Logo/Light Mode.jpg";
            }else{
                icon.src = "img/Logo/Dark Mode.png";
            }
        }

    </script>
 
Solution
It's much better to use localStorage rather than cookies for this.

Here's how to do it with localStorage:
JavaScript:
var icon = document.getElementById("icon");
var theme = localStorage.getItem("theme");

if (theme) {
    if (theme == "dark-mode") {
        document.body.classList.add(theme);
        icon.src = "img/Logo/Dark Mode.png";
    } else {
        icon.src = "img/Logo/Light Mode.jpg";
    }
}

icon.onclick = function() {
    document.body.classList.toggle("dark-mode");
    if (document.body.classList.contains("dark-mode")) {
        icon.src = "img/Logo/Light Mode.jpg";
        localStorage.setItem("theme", "light-mode");
    } else{
        icon.src = "img/Logo/Dark Mode.png";
        localStorage.setItem("theme"...
It's much better to use localStorage rather than cookies for this.

Here's how to do it with localStorage:
JavaScript:
var icon = document.getElementById("icon");
var theme = localStorage.getItem("theme");

if (theme) {
    if (theme == "dark-mode") {
        document.body.classList.add(theme);
        icon.src = "img/Logo/Dark Mode.png";
    } else {
        icon.src = "img/Logo/Light Mode.jpg";
    }
}

icon.onclick = function() {
    document.body.classList.toggle("dark-mode");
    if (document.body.classList.contains("dark-mode")) {
        icon.src = "img/Logo/Light Mode.jpg";
        localStorage.setItem("theme", "light-mode");
    } else{
        icon.src = "img/Logo/Dark Mode.png";
        localStorage.setItem("theme", "dark-mode");
    }
}
 
Last edited:
Solution

Buy us a coffee!

Back
Top Bottom