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

185
Vistas
TypeError no capturado: no se pueden leer las propiedades de undefined (leyendo 'setItem')

Quiero crear un sistema simple de modo claro/oscuro con javascript. Funciona configurando el booleano 'darkTheme' en Window.localStorage Aquí está el código que escribí:

 let lightThemeCheckbox = document.getElementById('lightThemeCheckbox'); let body = document.body; updateLightTheme(); lightThemeCheckbox.addEventListener('change', function() { Window.localStorage.setItem('darkTheme', this.checked); }); function updateLightTheme() { let darkTheme = window.localStorage.getItem('darkTheme'); if(darkTheme == undefined) { body.style.backgroundColor = 'rgb(255, 255, 255)'; return; } body.style.backgroundColor = darkTheme ? 'rgb(100, 100, 100)' : 'rgb(255, 255, 255)'; }

Pero por alguna razón, me sale este error:

 Uncaught TypeError: Cannot read properties of undefined (reading 'setItem') at HTMLInputElement.<anonymous> (header.js:7) (anonymous) @ header.js:7

¿Alguien puede ayudar?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Has puesto "ventana" con w mayúscula. Puedes usar lo siguiente:

Usando window minúsculas

 const lightThemeCheckbox = document.getElementById('lightThemeCheckbox'); const body = document.body; updateLightTheme(); lightThemeCheckbox.addEventListener('change', () => { window.localStorage.setItem('darkTheme', this.checked); }); function updateLightTheme() { const darkTheme = window.localStorage.getItem('darkTheme'); if (darkTheme == undefined) { body.style.backgroundColor = 'rgb(255, 255, 255)'; return; } body.style.backgroundColor = darkTheme ? 'rgb(100, 100, 100)' : 'rgb(255, 255, 255)'; }

Usando localStorage sin window

 const lightThemeCheckbox = document.getElementById('lightThemeCheckbox'); const body = document.body; updateLightTheme(); lightThemeCheckbox.addEventListener('change', () => { localStorage.setItem('darkTheme', this.checked); }); function updateLightTheme() { const darkTheme = localStorage.getItem('darkTheme'); if (darkTheme == undefined) { body.style.backgroundColor = 'rgb(255, 255, 255)'; return; } body.style.backgroundColor = darkTheme ? 'rgb(100, 100, 100)' : 'rgb(255, 255, 255)'; }

¡Esperaba que esto ayudara!

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