Exit Pop-Up with Javascript

This is a touchy topic because firing an action when people leave your site is usually offensive.  Most of the solutions I have seen are also spammy, using an alert() to ask whether someone is sure they want to leave the page.  But we were using a WordPress plug-in with a modal dialog that popped up after only a set number of seconds.  Our purpose was to find out if the person who had visited our site had found what they were looking for, and would they take a survey.

I ended up writing a script that would look at the link that they clicked and see if it was on our site or not.  If it was not, then it would run the pop-up.  The second aspect was to check to see if they were leaving our site without clicking a link, using the unload event.  The site uses jQuery so I was able to rely on that a bit.  The script also sets a cookie so that it won’t popup more than once per visit.

<script type=”text/javascript”>

$(document).ready(function() {

stayOnsite = false;

if (document.cookie.indexOf(“foobar”) != -1) {
return;
} else {

$(“a”).click(function() {

if (this.href.indexOf(“foobar”) !== whatever the indexOf is ) {

window.open(“http://foobar.com/survey.php”,”surveypopup”,”width=300,height=200,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no”);
document.cookie = “foobarSurvey=popped”;

} else {

stayOnsite = true;
}

});

window.onunload = surveyPopup;

function surveyPopup() {

if(stayOnsite) {

return;
}

window.open(“http://foobar.com/survey.php”,”surveypopup”,”width=300,height=200,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no”);
document.cookie = “foobarSurvey=popped”;
}

}
});

</script>

I got parts of this script from another site, which I can no longer identify in my history.  The rest is pieced together from a variety of places.  The script has a couple of problems:

  • the unload function doesn’t always work, and it seems to be browser specific (Chrome okay, not IE).  I have read a lot about the unload and beforeunload events not always firing based on browser support;
  • if the visitor is on IE and has popups blocked, and the link is opening in a new window, the popup in this script will work but the popup for the page link will be blocked.  The visitor can then click on the link again (and the survey popup will not fire because a cookie has been set) and the content popup will open but it’s obviously not desirable.

If anyone has any suggestions on improving this script or why it is flaky, I’d be curious.  I’m not a developer so I’m bolting bits and pieces together.  I don’t care for the paid exit pop-up plugins for WordPress and am leaning towards the solution that I have seen recommended, which skips the pop-up and just creates a black bar at the top of the page asking whether the visitor would be willing to take a survey at the end of the visit.  That seems the least intrusive and the most manageable.  But I thought I’d share this scripting, for what it’s worth, in case anyone can use it.

Leave a Reply

Your email address will not be published. Required fields are marked *