Page exit popup

What I'm trying to do is create a popup anytime the page comes out or is deleted. Right now i have

<script type="text/javascript"> function box() { var r=confirm("Message"); if (r==true) { window.location.href="yes.html"; } else { window.location.href="no.html"; } } </script> <body onunload="box();"> 

I have 2 problems with this:

  • It shows only the field, if you really move far from the page, refresh, new url, etc. If you exit a tab or browser, a window will not appear.

  • No matter which button you click, it just sends you where you tried to go initially, it never sends you to no.html or yes.html .

Can someone tell me how this is possible?

+4
source share
1 answer

Try the following:

 <script type="text/javascript"> window.onbeforeunload = confirmExit; function confirmExit() { setTimeout(function() { setTimeout(function() { window.location.href="yes.html"; }, 1000); },1); return "Message"; } </script> 

You can catch the option to stay on the page; you cannot override the user leaving the page. similar: A way to find out if a user has clicked Cancel in the onbeforeunload Javascript dialog box?

+7
source

Source: https://habr.com/ru/post/1435816/


All Articles