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

129
Visualizações
How Do I go back to my html page without losing the index of it?

I have two html page. From Page 1 I can go to page 2 and from page 2 I can go back to Page 1.

In page 1 I have a button that display an Image. When I go to page 2 and back to page 1 my image disappear. Is there a way to not lose what I did on page 1 when I go back to it?

<html>

<head>
    <title>
        Onclick javascript to make browser go
        back to previous page?
    </title>
</head>

<body>
<button onClick="showImage()">Button</button>
<div id="first" style="height:200px; width:200px; display:none;">
    <img src="PP.jpg"/>
</div>

<script>
    const showImage = () => {
        document.getElementById("first").style.display ='block';
    }
</script>

    <h1 style="color: green">
        GeeksforGeeks
    </h1>

    <b>
        Onclick javascript to make browser
        go back to previous page?
    </b>

    <h2>Page 1</h2>

    <p>
        Click on the link to get
        to the second page.
    </p>

    <a href="Page2.html">Go to Page 2</a>
</body>

</html>

I am currently using onclick="history.back()" for it refresh my starting page and remove the image

<!DOCTYPE html>
<html>

<head>
    <title>
        Onclick javascript to make browser
        go back to previous page?
    </title>
</head>

<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>

    <b>
        Onclick javascript to make browser
        go back to previous page?
    </b>

    <h2>Page 2</h2>

    <p>
        Click on the button to go
        back to the previous page.
    </p>

    <button onclick="history.back()">
        Click here to go back
    </button>
</body>

</html>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to save the state in a storage, for example a sessionStorage. You can handle it with the onload Event.

You need to save the state in the storage when the button is clicked to show the image. And when the page reloads, check if the button is clicked, if so then show the container else don't show.

const showImage = () => {
 sessionStorage.setItem('showImage', 'true');
 document.getElementById("first").style.display ='block';
}
    
function myFunction() {
  if (sessionStorage.getItem('showImage') === 'true') {
   document.getElementById("first").style.display ='block';
   sessionStorage.removeItem('showImage');
  }
}

on the body tag add this:

<body onload="myFunction()">

For example: https://jsfiddle.net/dn09ysa1/

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use sessionStorage :)

const showImage = () => {
  document.getElementById("first").style.display ='block';
  sessionStorage.setItem("imageState", "visible");
}
(sessionStorage.getItem("imageState") === "visible" ? document.getElementById("first").style.display ='block' : document.getElementById("first").style.display ='none');
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