Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda