Event for when the user leaves the page

Asked

Viewed 3,085 times

1

Would you like to make a template equal to that of the free ratchet site. How could you do this? It consists of when the mouse leaves the page appear a alert wondering if the user really wants to leave the page. What javascript event does that someone knows ?

  • you can send the URL to demonstrate the desired behavior?

  • https://catracalivre.com.br/curitiba/agenda/barato/fernando-meirelles-debate-os-novos-rumos-do-cinema-no-teatro-bom-jesus/

  • There is no such event. The algorithm used on this site is very well worked, as far as I could see he considers scroll page, permanence time and mouse tracking, the solution is to do reverse engineering. Note: this is not a alert(), but rather a modal (example)

2 answers

1

I believe you are looking for this event:

window.onbeforeunload = function(){return "Deseja mesmo sair do site?"};

This will display a confirmation window when the user clicks to close the tab.

  • Is it possible to put Alert and not be a confirmation window for example just put an Alert to warn him? if you have how I believe it would be this event even if when you pass the mouse in the tab it gives that information

  • not that way I want I just want an Alert when it passes the mouse on the tab just want q displays that an Alert when it passes the mouse on the tab

1

Although the question is from some time ago, I realized I have no answer that really answer the question, so for those who have the same doubt and end up falling in this post.

Just use the event mouseleave of Jquery in the body.

$('body').mouseleave(function(){
    alert('O Usuário tirou o mouse da página');
})

In place of Alert you do whatever is necessary, such as putting a pop up to "hold" the user on the page.

Browser other questions tagged

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