I've mentioned this a few times:
To summarize the answers, onunload and onbeforeunload do not serve to detect when the person leaves the site, what he does is detect the download , that is, a link from the same site will trigger the event, pressing F5 will trigger the event, if you use Back or Forward will trigger the event.
If you want to use it, it is necessary to first understand some things, when unload or beforeunload occurs the page is already preprocessed to destroy / unload and therefore new windows / GUIs will not be generated.
That is, alert()
, confirm()
, prompt()
and window.open()
will not run, even if you wish, this is explained in the W3C specification link
note: alert / confirm and others are also ignored in event pagehide
Then the only event you should use is beforeunload
with return '<string>';
, there is one more problem your HTML is badly marked, this does not affect execution, but it should still be this:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script>
//script vai aqui
</script>
</body>
</html>
However note that even using:
window.onbeforeunload = function(){
return 'Tchau';
};
Will not send the desired text , it will just generate a default message asking whether or not to download the page:
Inotherwords,thisisnota"goodbye" message, nor is Tchau
displayed, it's a resource to be used on certain occasions, to say goodbye for sure is not a good way to implement this. / p>
Providing a good experience for the user
Say hello, say goodbye, is this really necessary? Does the user not know that they are already leaving or when they have entered the page?
I personally believe that these types of messages sound more like noise than something useful to the user, so much so that for many of these problems certain implementations have been removed from browsers, many more things cause that , again this message ", than to actually appear polite, the user will feel welcome on the site when he gets what he wants from the site, not with a disturbing welcome message that locks everything until it is pressed the ok
button.
On the use of beforeunload
and unload
I have explained, they are not to check when the person leaves the site, and there is no guaranteed way to do this, you can implement anything, detect internal and external links, but this kind of thing is unnecessary work, I personally recommend that you focus on creating a nice interface to navigate so that the user feels good when surfing, so yes he will feel welcome and when he leaves the site he will surely be an hour will want to come back.
What would the utility of beforeunload be?
It would be more interesting if it was used to prevent it from losing editions, or if it is a game to prevent it from leaving without saving the game, for this is exactly what the default message says:
Do you want to leave this site?
It is possible that changes you have made have not been saved
This is his focus today, to prevent the user from accidentally closing something and forfeiting something.