7

Bounce Exchange has figured out an almost perfect way of detecting if a user will leave the website. They do this based on tracking mouse gestures, mouse velocity, and breaking of the browser plane. If they detect someone is leaving they fire off a popup on a lightbox.

I can poorly emulate this, by the following:

$("body").mouseleave(function() {
    jQuery('#avoid-bounce').show();
});

The only problem is this is rather annoying. Even if it captures someone, the moment they leave the body it fires again.

How probable would it be to factor in mouse speed and allow the event to fire only once? I'm still fairly new to JavaScript and jQuery, but I'm learning.

1
  • just added bounceexchange.com to my adblock list ;) Mar 3, 2014 at 10:23

3 Answers 3

9

This is exactly what .one() is for:

$("body").one('mouseleave', function() {
    jQuery('#avoid-bounce').show();
});
3

You can add a flag to your code:

$("body").mouseleave(function() {
    if ( jQuery('#avoid-bounce').data('shown') != true ) {
        jQuery('#avoid-bounce').data('shown', true).show();
    }
});

Creating a flag will make sure the show() code will not be called the second time.

1
  • Vlad, Can you please put a example on fiddle will be really helpful for me and more people looking for something like this. Jan 3, 2014 at 12:26
2

Or you can try OuiBounce,the bounce exchange alternative: https://github.com/carlsednaoui/bounce-exchange-alternative

2
  • THANK YOU!!!!!!!!!!!!!! I've been googling for EVER to find a free alternative. I should give you 100 upvotes.
    – Tallboy
    Apr 8, 2014 at 18:44
  • ouiBounce is a good alternative, but unfortunately doesn't work well. If you have small things at the top, it will fire the pop-up. It should take into consideration the speed of the mouse. Apr 28, 2014 at 15:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.