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

141
Visualizações
How to prevent users from leaving the site with the an exit popup? JS

I want to be able to have a copy of my website to reappear every time the user tries to leave the site. Is there such a method/event handler?

No need to mention the bad UI, I just want to explore more with event handlers

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

0

You could use the onbeforeunload event.

window.onbeforeunload = function() {
   return 'Are you sure that you want to leave this page?';
};
about 4 years ago · Juan Pablo Isaza Relatório

0

There is no way to prevent a user from leaving your website, let alone spawn a new tab upon them exiting.

Here is a similar question that discusses why this is a bad idea and why most modern browsers have since removed this ability:

"How to block users from closing a window in Javascript?"

The example below is the purely theoretical:

<!html>
<html>
  <head>
    <title>Persistent Page...</title>
    <script type="text/javascript">
      function closeHandler(e) {
        e.preventDefault(); // Does not actually cancel
        e.returnValue = ''; // Does prevent dialog (doesn't show anyways)
        alert('Leaving website, opening a new tab...'); // Does not display
        window.open(window.location.href, '_blank'); // Open a new tab?, No...
      }

      if (window.addEventListener) {
        window.addEventListener('beforeunload', closeHandler, true);
      } else if (window.attachEvent) {
        window.attachEvent('onbeforeunload', closeHandler);
      }
    </script>
  </head>
  <body>
    <h1>Persistent Page...</h1>
  </body>
</html>
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