Skip to content

Instantly share code, notes, and snippets.

@crates
Created April 2, 2019 17:27
Show Gist options
  • Save crates/4a18b54643bab5fd0d947b9d13a68c74 to your computer and use it in GitHub Desktop.
Save crates/4a18b54643bab5fd0d947b9d13a68c74 to your computer and use it in GitHub Desktop.
Drupal / jQuery Exit Modal: Show the user a modal when they are attempting to leave your website
(function($, Drupal) {
"use strict";
var exitModalCookie = $.cookie("__exitModal");
// set up exit intent listener to fire modal
var exitIntent = function(e) {
if (e.clientY < 0 ) {
// init remodal
var exitModal = $('[data-remodal-id=exit-modal]').remodal();
exitModal.open();
// prevent sowing this more than once per page
document.removeEventListener('mouseleave', exitIntent, false);
}
}
// if user is logged out. Fire that exit modal.
if (!exitModalCookie) {
document.addEventListener('mouseleave', exitIntent, false);
$.cookie('__exitModal', 1, {path: '/', expires: 1});
}
})(jQuery, Drupal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment