Create an Exit Intent Popup Window

In this tutorial we’re going to create an exit intent popup window. First, we’ll talk about what exit intent is, then I’ll walk your through the steps of creating the popup window, then we’ll create the exit intent script.

We’re going to use the modal window style from a previous tutorial: pure css modal window


What is Exit Intent?

Exit intent is a technology that tracks when the visitor is about to leave the website and then it does something. For example a subscription box popups up or something similar. Many sites use this technology to increase email subscribers and get back visitors who are about to leave.

The basic idea behind this is, that there’s a JavaScript that tracks the size of the browser window, and if the cursor leaves that box, it triggers an other function.

Here’s a tutorial on how this works: Trigger a Function on Mouse Leaving the Browser with jQuery

Creating the Modal Window

First, we need to create the modal window itself. I won’t go into details about this here, you can check the tutorial about for a more detailed explanation on this.


<div class="lightbox" id="text">
    <div class="box">
        <a class="close" href="#">X</a>
        <!-- your title -->
        <div class="content">
        <!-- your content -->


.lightbox {
    /** Hide the lightbox */
    display: none;

    /** Apply basic lightbox styling */
    position: fixed;
    z-index: 9999;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;

.lightbox:after {
    content: '';
    display: table;
    clear: both;

.lightbox .box {
    width: -webkit-min-content;
    width: -moz-min-content;
    width: min-content;
    margin: 2% auto;
    box-shadow: 0px 1px 26px -3px #777777;

    .lightbox .title {
        padding:0 0 10px 0px;
        border-bottom:1px #ccc solid;

    .lightbox .content {
        padding:10px 0 0 0px;

    .lightbox .close {
        font-family:Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;

.lightbox .open {
    /** Show lightbox when mouse leaves the browser window */
    display: block;
    outline: none;

Triggering the Popup with Exit-Intent

For us to be able to use this method, we’ll need to include jQuery into our project.

<script src=""></script>

After we have that, we can move on to the script. I’ll explain below.

// Exit intent
function addEvent(obj, evt, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evt, fn, false);
    else if (obj.attachEvent) {
        obj.attachEvent("on" + evt, fn);

// Exit intent trigger
addEvent(document, 'mouseout', function(evt) {

    if (evt.toElement == null && evt.relatedTarget == null ) {


// Closing the Popup Box

First, we create a tracks if the mouse is still on the browser window, then we we trigger it with an if statement. If it’s triggered, we slide down our popup window. After that, we create a short script, that if we click the close button the popup slides back up.

And that’s it, we have our exit-intent triggered popup window.

Please note that we didn’t add any cookie system to track if the popup was closed, so each time you close it, and move the mouse out the window, it’ll popup. We’ll cover cookies in a future tutorial.