Display Popup when realizing that the user will leave the page

0

I want to display a PopUp to display a form to capture the email and the name of the user who is accessing the site at the time it is close to leaving the site. I see this commonly on several landings pages, especially those focused on lead conversion. Whenever I drag the mouse arrow to the close button of the browser popup appears.

    
asked by anonymous 23.08.2017 / 23:50

1 answer

1

In this case, what you want is the event of when the user takes the mouse out of the window. For this you can use a solution like this:

Create a function that applies cross-browser mode event:

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

And then apply the event to when the mouse exit in the window:

addEvent (document, "mouseout", function (e) {     e = e? e: window.event;     var from = e.relatedTarget || e.toElement;     if (! from || from.nodeName == "HTML") {         // Here you put your popup         alert ("left window");     } });

Here is the complete sample code:

function addEvent(obj, evt, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evt, fn, false);
    }
    else if (obj.attachEvent) {
        obj.attachEvent("on" + evt, fn);
    }
}
addEvent(window,"load",function(e) {
    addEvent(document, "mouseout", function(e) {
        e = e ? e : window.event;
        var from = e.relatedTarget || e.toElement;
        if (!from || from.nodeName == "HTML") {
            // Aqui você coloca o seu popup
            alert("left window");
        }
    });
});
    
24.08.2017 / 00:28