Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!