Fire exit pop UNLESS a specific button is clicked

I am working on a promotion, where I want to capture someone who is leaving a specific page without adding an item to their cart.

Thus, if they leave that page by clicking the add to cart button, don't fire the exit pop (which offers them a coupon or a discount).

If, however, they exit the page by any other means, I want to have an exit pop that offers them the discount.

I have the exit pop working, but unfortunately, it fires even if they are clicking the add to cart button. So it always offers them the discount.

How do I alter this code so it DOES NOT fire when the visitor clicks on the add to cart button?
<script type="text/javascript">
		function PopIt() { 
			window.onbeforeunload = UnPopIt;
			return "Wait! Don't leave. Click "Say on this page" or "Cancel" and you'll recieve a 50% discount!"; 
		function UnPopIt()  { /* nothing to return */ } 

		$(document).ready(function() {
			window.onbeforeunload = PopIt;

				'hideOnContentClick': false,
				'showCloseButton': false

			$("a[id!=trigger]").click(function(){ window.onbeforeunload = UnPopIt; });

Open in new window

(FYI... the add to cart button is a form. It uses's simple checkout:

<form name="PrePage" method = "post" action = ""> <input type = "hidden" name = "LinkId" value ="767512c3-540d-48e9-8197-xxxxxxxxxxxxxxxx" /> <input type = "image" src ="/images/addtocart-style2.png" /> </form>

Open in new window

LVL 32
Who is Participating?
leakim971Connect With a Mentor PluritechnicianCommented:
not really, add :

<input type = "image" src ="/images/addtocart-style2.png" onclick="displayIt = false;" />
var displayIt = true;
function PopIt() {
if( displayIt ) {
                  return "Wait! Don't leave. Click "Say on this page" or "Cancel" and you'll recieve a 50% discount!";


did not see clearly your onclick event for your addcart button/link but just set inside : displayIt = false;
DrDamnitAuthor Commented:
That's because there is no onclick function. It is just a form with submit as shown in the sample code below the sample code.

Would an onclick event help?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.