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

90
Visualizações
javascript beforeunload listener not firing properly

I have a very simple html file with a javascript snippet that contains a beforeunload event listener:

<!DOCTYPE html>
<html lang="en">
  <body>
    hello world
  </body>
  <script>
    window.addEventListener("beforeunload", (event) => {
      alert("hello");
    });
  </script>
</html>

Per mozilla's beforeunload documentation, I expected to see an alert('hello') pop up when i close my tab. Instead, the tab closes without anything else happening. Does anyone know what I might be doing wrong? Thanks!

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

0

Per the mozilla documentation, for chrome you must also set the return value.

<!DOCTYPE html>
<html lang="en">

    <body>
        hello world
    </body>
    <script>
        window.addEventListener("beforeunload", (event) => {
            event.preventDefault()
            event.returnValue = '';
            alert("hello");
        });
    </script>

</html>

The issue you might be facing is, once opening the page, you need to click in the page to make it active, then once you click to close the tab, you should get the "Are you sure" message.

Although, even then... The alert doesn't work. This would no doubt relate to the small note at the top of the documentation saying its to prevent annoying popups.

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