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

132
Visualizações
How to trigger a function on press of Back button of window?

Trying to trigger a function (let say an alert) with the press of the back button (every time).

Code goes as:

$(function() {
  if (window.history && window.history.pushState) {
    window.history.pushState('', null, './');
    $(window).on('popstate', function() {
      alert('Back button was pressed.');
    });
  }
});
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

However, the above code works but only once on page load, to make it work again, I have to reload the page again, hence the function runs again so that it detects the "back press".

To overcome this, I tried calling the function in the same function.

function call() {
  if (window.history && window.history.pushState) {
    window.history.pushState('', null, './');
    $(window).on('popstate', function() {
      alert('Back button was pressed.');
      call();
    });
  }
}

$(document).ready(function() {
  call();
});
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

This time, the on-page load, the call() gets called and triggers the alert on the back press. But the second time pressing on back, the alert gets called twice, on 3rd time press of a back button the alert gets triggered 4times and so on...

Here's the JSFiddle to play around.

There is some logic I seem to be missing. Any help is appreciated.

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

0

After some JSFiddling, I kinda manipulated my code to work the way I need.

the update wouldn't justify the above query but fulfilled my requirent.

Here's the code:

<div class="some" style="width:100px;height:50px;background:green;display:none;"></div>

<button class="some_btn">tada</button>

<script>
$(".some_btn").click(function() {
    $(".some").css("display","block");
  call();
});

function call() {
  if (window.history && window.history.pushState) {
  if($('.some').css('display') == 'block') {
    window.history.pushState('', null, './');
    $(window).on('popstate', function() {
      $(".some").css("display","none");
    });
  } else { 
  
  }
    
  }
}
</script>

so in the above code, the call() gets triggered every time that particular block is displayed and made the "back button" null and upon hiding the block, the back button gets enabled.

Hope this helps the one in need.

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