You should use this:
window.onbeforeunload = function(){
return "Hello"
}
A window's onbeforeunload property may be set to a function that returns the string that is shown to the user in a dialog box to confirm that the user wants to navigate away. This was intended to prevent users from losing data during navigation. Unfortunately, it is often used to scam users.
Here it says basically that you have to return a string to the user, so he can decide whether he will quit or not. more ...
Starting in Chrome 51, the custom string will no longer be shown to the user. Chrome will still show a dialog to prevent users from losing data, but it's contents will be set by the browser instead of the webpage.
From google Chrome 51, returning strings is no longer allowed, that is, you can even, but chrome will display their default message.
Explaining page closure
//MEUS ARQUIVOS
/
/index.js
/fechou.php #SERÁ EXECUTADO QUANDO A APLICAÇÃO SAIR DA MEMORIA RAM/CPU
/index.html
//fechou.php
<?php
$name = $_POST['name'];
file_put_contents("pessoas_que_fecharam.txt", $name);
//index.js
window.onbeforeunload = function () {
return "Não será apresentado na tela";
}
window.onunload = function () {
//essa evento é executado depois de window.onbeforeunload.
//Quando a página é fechada, o navegador executa isso aqui(POST)
//quando o post é executado, o navegador tira tudo da memoria RAM
//e processador
$.post("fechou.php", {name: "MARCELO"});
}
<!--INDEX.HTML-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><scripttype="text/javascript" src="index.js"></script>
</body>
</html>
First
When the user clicks to close the page / tab, window.onbeforeunload
is executed and then a message appears, asking if the user wants to exit or not. This is my window.onbeforeunload
window.onbeforeunload = function () {
return "Não será apresentado na tela";
}
Second
When the user decides to leave the page, window.onunload
is executed.
window.onunload = function () {
//essa evento é executado depois de window.onbeforeunload.
//Quando a página é fechada, o navegador executa isso aqui(POST)
//quando o post é executado, o navegador tira tudo da memoria RAM
//e processador
$.post("fechou.php", {name: "MARCELO"});
}
When window.onunload
is executed / terminated, the browser removes everything from memory. And I should have a file named pessoas_quechamaram.txt
with the name MARCELO
inside.
Sources:
link
link
link