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

134
Visualizações
Check variable and send to home page with JS

hello I'm using this code to send variables to another page

    function store () {
  var first = "Foo Bar",
      second = ["Hello", "World"];
  localStorage.setItem("first", first);
  localStorage.setItem("second", JSON.stringify(second));
  location.href = "http://example/page2.html";
}

How can I check if the variable exists in PAGE2 and if no there, how can I redirect to homepage(PAGE1)?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to check localStorage in Page2 if variables are not set, localStorage.getItem will return null and if variables are null redirect to the page1

Page 2

script

<script>
    var first = localStorage.getItem("first");     
    var second = localStorage.getItem("second");

    // if first and second variables are false then redirect  
    if(!first && !second) {
        location.href = "http://example/page1.html";
    }
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

The local storage is shared between all the pages of a website. So when you do localStorage.setItem("key","value") this key value pair is accessible in PAGE 2 using localStorage.hasOwnProperty("key") to check if it exists and localStorage.getItem("key") to retrieve its value.

On PAGE1

if(!localStorage.hasOwnProperty("key")){
    // do your logic
}

If PAGE2 is not on the same website, i.e not stored under the same url; you have no direct way to access/verify the stored value (unless you use a library).

Check the following question for the same issue :

cross domain localstorage with javascript

enter image description here

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