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

349
Visualizações
Click a button when the page reloads

When I click a button (#test-element) it makes the page refresh but I want it to then run a function after the page is refreshed to open the cart by clicking a button automatically(.cart-contents). How do I run a function after the page is refreshed if the add to cart button (#test-element) was clicked. So the page refreshes and clicks another button(.cart-contents)?

<script>
 $("#test-element").on("click",function() {
 setTimeout(function () {
 location.reload(true);
 }, 500);
 });
</script> 
<script type="text/javascript">
  $("#test-element").on("click",function() {
  setTimeout(function () {  
  $(document).ready(function(){
  $(".cart-contents").trigger('click'); 
  }, 500);
  });});
</script>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use sessionStorage (only applied to the current session page) to keep your clicking state. After the page is reloaded, we can remove that temporary storage.

You can try this sandbox for the test

$(document).ready(function() {
  $("#test-element").on("click", function() {
    setTimeout(function() {
      sessionStorage.setItem('isReloaded', 'true');
      location.reload(true);
    }, 500);
  });
  $(".cart-contents").on("click", function() {
    alert('cart-content triggered')
  })
  const isReloaded = sessionStorage.getItem('isReloaded');
  if (isReloaded) {
    sessionStorage.removeItem('isReloaded')
    $(".cart-contents").trigger('click');
  }
});

This way is not perfect but it can solve your basic needs in terms of keeping page states.

about 4 years ago · Juan Pablo Isaza Relatório

0

You probably don't need (or even want) to actually do this. It sounds like an over-complicated solution to a problem of mixing AJAX and page loads/requests, and the real solution would be to fix whatever design problem is causing you to want to do this to compensate for it.

Having said that...

Each time the page loads, the JavaScript starts anew. So any information you want to persist between page loads needs to exist outside of the page code itself. One such place to persist data could be localStorage.

So your operation to reload the page would write a value to localStorage:

$("#test-element").on("click",function() {
  setTimeout(function () {
    localStorage.setItem("clickCartContents", true);
    location.reload(true);
  }, 500);
});

And when loading the page you can check to see if that value was set:

$(document).ready(function(){
  if(localStorage.getItem("clickCartContents")) {
    $(".cart-contents").trigger('click');
    localStorage.removeItem("clickCartContents");
  }
});
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