Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popup box on page exit

What I am trying to do is make a pop-up box any time a page exits or is navigated away from. 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:

  1. It only shows the box if you actually navigate away from the page, refresh, new url etc. If you exit the tab or browser, the box doesnt pop up.

  2. No matter what button you press, it just sends you where you tried to go originally, it never sends you to no.html or yes.html.

Could someone tell me how this is possible?

like image 624
CJ Sculti Avatar asked Sep 24 '12 00:09

CJ Sculti


People also ask

What is an exit pop?

An exit popup is a popup that appears on the user's screen when he or she attempts to leave the site. It's a last-ditch effort to keep your prospective customer on the page.

Why does exit intent pop up?

An exit intent popup appears on a user's screen when they seem like they're about to leave a website. Usually, an exit popup will make an offer to draw a user back to the site. In other words, exit intent pop-ups know when one of your visitors is about to close their browser window.

Do exit intent pop ups work?

If you target the right pages and the right audience, an exit intent overlay can be an excellent opportunity to show a special offer of your brand to your visitors. Exit popup window works very well in landing pages, commercial websites, and online eCommerce stores.


1 Answers

Try this:

<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 only catch the stay on the page option, you cannot override a user leaving the page. similar to: Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?

like image 198
Glennular Avatar answered Oct 20 '22 00:10

Glennular