Doubt Warning in Javascript

Asked

Viewed 228 times

2

I was looking for a function to display an alert message to the user when he leaves the page and I located this one on the forum:

<script>
   // Script de Alerta ao fechar a pagina... 
   window.onbeforeunload = fecharJanela 

   function fecharJanela(){  
      return "Você realmente deseja sair? os dados não salvos serão perdidos..."; 
   } 
</script>

How to make this alert not run when I perform a Submit?

The problem is happening because the system I am developing is performing a refresh on the ai page when clicking the button that sends the data the browser is displaying the message

Complementing the Post.... Look at the image, the warning message is displayed when I click on "change" I wanted it not to be displayed when I clicked on that button

inserir a descrição da imagem aqui

  • missed you put the message in the question

  • What message? Make submission through isset($_POST)? That phrase made no sense. Make the alert not run? What do you mean? Overall, your question made no sense. You can [Dit] and try to improve it by better describing the problem?

  • complemented with an image! is displayed an alert in the browser..

  • create a Boolean, and only run the Alert function if that Boolean is true or false depends on its logic

3 answers

2


That’s because your button must have some hyperlink or Submit to leave the current page which makes sense.

To solve this problem just undo the function onbeforeunload at the event onclick button:

document.getElementById('alterar').onclick = function () {
    window.onbeforeunload = null;
};

Or you can even associate in HTML, example:

<button type="submit" id="alterar" onclick="window.onbeforeunload = null;">Alterar</button>
  • thanks! I put the informed in onclick and it worked

1

If there is a button then set the window.onbeforeunload for null when it is clicked, so the alert will not be displayed.

It doesn’t make sense to want to do it like isset($_POST[])), this is PHP, while the alert is already in the client, in Javascript.


That will work:

SeuElemento.addEventListener("click", function(){
    window.onbeforeunload = null;

    //... Resto do código do botão
});

Test here. (code)

  • thanks for the tip! do not want to do via PHP, actually put isset[$_POST] to show how I am sending the data via Submit...

1

window.onbeforeunload = function () {
  return 'Blá Blá Blá';
}

function enviar()
{
   window.onbeforeunload = null;
   return true;
}
<form>
    <input type="text" name="name">
    <input type="submit" name="submit" onclick="enviar()">
</form>

You can add a condition to your Function or clear the window.onbeforeunload event.

Example of removing the event:

window.onbeforeunload = null;

Condition example:

<script>
   // Script de Alerta ao fechar a pagina... 
   window.onbeforeunload = function(){  

     if(isFormSubmit)
        return;

     return "Você realmente deseja sair? os dados não salvos serão perdidos..."; 
   } 
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.