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

107
Vistas
localstorage is not working after window.location.href

I am setting a value checked using localstorage.setitem and using window.location.href to redirect to another page. When page loads and I am trying to acess the saved value using localstorage.getitem, it is not working.

Here is my code:

<input type="checkbox" name="names" onchange="submit(this)" />
 function submit(element) {
                            var names = document.getElementsByName('names');
                            var selectedList = [];
                            for (var i = 0; i < names.length; i++) {
                                if (names[i].checked == true) {
                                    window.localStorage.setItem(names[i].checked, "checked");
                                }
                            }
                            window.location.href = "/testpage.html";
                            
                        }
                        $(document).ready(function () {
                            var names = document.getElementsByName('names');
                            for (var i = 0; i < names.length; i++) {
                                if (window.localStorage.getItem(names[i].checked) == "checked") {
                                    names[i].setAttribute('checked', 'checked');
                                }
                            }
                        })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Let's say you're on page x.html and you're redirecting to another page from this page using this line window.location.href = "/testpage.html"; So any code written in javascript of x.html after the redirecting will not execute, because the execution context of JS now moves to javascript written for testpage.html. With that said, the solution to your problem will be, to set the localStorage from x.html's JS and when redirected to testpage.html the handle the getting from localStorage part there and then set the values gotten fro localStorage to the checkboxes.

Let's see example of what i've explained above. Your x.html should look something like this:

 function submit(element) {
      var names = document.getElementsByName('names');
      var selectedList = [];
      for (var i = 0; i < names.length; i++) {
          if (names[i].checked == true) {
              window.localStorage.setItem(names[i].checked, "checked");
          }
      }
      window.location.href = "/testpage.html";
  }
<input type="checkbox" name="names" onchange="submit(this)" />

And javascript for testpage.html should look something like this:

$(document).ready(function () {
    var names = document.getElementsByName('names');
    for (var i = 0; i < names.length; i++) {
        if (window.localStorage.getItem(names[i].checked) == "checked") {
            names[i].setAttribute('checked', 'checked');
        }
    }
})

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