Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
I have a switch theme button, on my website, how can i save the theme when refreshing, or going to an other pages?

I created a switch theme button for my website, it only modify the background color of the body and the header.

The html :

<div class="buttonContainer">
    <button onclick="SwitchTheme()"></button>
</div>

The js :

function SwitchTheme() {

                   var element = document.querySelector("body");
                   element.classList.toggle("dark-mode");

                   var elementB = document.querySelector("header");
                   elementB.classList.toggle("nav-dark-mode");


                   var x = document.getElementById("Sun");
                   var y = document.getElementById("Moon");

                      if (x.style.display === "none") {
                        x.style.display = "block";
                        y.style.display = "none";
                      } 

                      else {
                        x.style.display = "none";
                        y.style.display = "block";
                      }

                      console.log("VarSampleVal")
                }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I suggest you store theme a class on cookie, using javascript cookie. so that when the class name existed on the cookie use it as a default theme class.

Then everytime your page reloaded/refreshed, check on the cookie of what class is stored and print it on your class tag.

HTML


<script type="text/javascript">
function SwitchTheme() {

   var element = document.querySelector("body");
   element.classList.toggle("dark-mode");

   var elementB = document.querySelector("header");
   elementB.classList.toggle("nav-dark-mode");


   var x = document.getElementById("Sun");
   var y = document.getElementById("Moon");

      if (x.style.display === "none") {
        x.style.display = "block";
        y.style.display = "none";

        // Store Dark Mode Class in Cookie
        document.cookie = "my_theme_body_class=dark-mode; path=/";
        document.cookie = "my_theme_header_class=nav-dark-mode; path=/";
      } 

      else {
        x.style.display = "none";
        y.style.display = "block";


        // Delete Value Dark Mode Class in Cookie
        document.cookie = "my_theme_body_class=; path=/";
        document.cookie = "my_theme_header_class=; path=/"; 
      }

      console.log("VarSampleVal")
}

function getCookie(cname) {
  let name = cname + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(';');
  for(let i = 0; i <ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

var el_body = document.querySelector("body");
el_body.classList.add(getCookie('my_theme_body_class'));

var el_header = document.querySelector("header");
el_body.classList.add(getCookie('my_theme_header_class'));
</script>


</body>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda