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

489
Visualizações
How to disable beforeunload action when user is submitting a form?

I have this little piece of code:

<script>
$(window).bind('beforeunload', function() {
  $.ajax({
    async: false,
    type: 'POST',
    url: '/something'
    });
  });
</script>

I wonder, how could I disable this request when user hits the submit button.

Basically something like here, on SO. When your asking a question and decide to close the page, you get a warning window, but that doesn't happen when you're submitting the form.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Call unbind using the beforeunload event handler:

$('form#someForm').submit(function() {
   $(window).unbind('beforeunload');
});

To prevent the form from being submitted, add the following line:

   return false;
over 4 years ago · Santiago Trujillo Relatório

0

Use

$('form').submit(function () {
    window.onbeforeunload = null;
});

Make sure you have this before you main submit function! (if any)

over 4 years ago · Santiago Trujillo Relatório

0

This is what we use:

On the document ready we call the beforeunload function.

$(document).ready(function(){
    $(window).bind("beforeunload", function(){ return(false); });
});

Before any submit or location.reload we unbind the variable.

$(window).unbind('beforeunload');
formXXX.submit();

$(window).unbind("beforeunload"); 
location.reload(true);
over 4 years ago · Santiago Trujillo 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