Alert when closing the browser

Asked

Viewed 2,454 times

1

As I would make an alert appear when closing the browser, I got one, but it also gives the alert when changing tabs and that’s not what I want at the moment....

Thank you!

  • You want the message to appear when you close the tab tbm?

2 answers

3

I don’t know if it helps... but you can use this method... the only problem is that it will be executed while doing refresh, etc...

<script type="text/javascript">
window.onbeforeunload = confirmar_saida;

function confirmar_saida()
{
    return 'Vamos ter saudades suas!';
}
</script>

Olá! :D
  • Right Tiago. But you can direct to another page?

  • In this case you can use window.Location to redirect...

1

window.location ao clicar em um botão   
function redirecionar(){ 
    alert("Você será redirecionado"); 
    window.location="sair.php/"; 
}

Or

function funcao() {
 var r = confirm('Quer mesmo sair?');;

 if (r == true) {
    window.location.assign("http://www.globo.com") /* ou o seu sair.php */
 } 
else {
    alert('Nao funcionou');
}
 return false;
}
<a href="#" onclick="return funcao();">Sair do sistema</a>

  • Perfect Paul. How would I redirect to another page? put under the Return window.location.href='page.html'?

  • How will you redirect if you will close the browser? N got it...

  • In the system, the user when logging in the status is online, but when he clicks "Sign out" from the system, I direct him to the home page and change the status to offline. Only that many users are closing directly through the browser and the status is online, even if it is not... would need to appear the message and redirect to the exit page. As for example in bank sites when we press F5

  • Okay. I get it, I’m going to update the answer

  • I got the code I put, but when accessing the site, it already appears the message...

  • See now how it turned out :)

  • Hi Paulo. So, actually it would be to exit the browser or close the tab where the system is... I put the onunload() on the body, but it did not work

  • put tb onbeforeunload() on body, also did not work.

  • I think your logic is wrong friend. How will I redirect the user if he I click out of the system? I think the right one is to do as I showed in the example :)

  • Try to redo using the method I showed in the answer, I think the system will be even more consistent.

  • Right Paulo. Actually, when you click the exit button on the system, there is already a confirmation message. Only I am not able to put this message when closing the browser or tab of the browser, otherwise the user will remain online, even if no longer....

  • From what I’ve researched, I think with jquery it works.. I’m looking for a solution and post here...

  • I got this code in jquery, but it’s not working: $(window). bind('beforeunload', Function(e) { // Your code and validation if (confirm) { Return "Are you sure?" ; } });

Show 8 more comments

Browser other questions tagged

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