Display Popup when you realize the user will leave the page

Asked

Viewed 1,847 times

1

I am willing 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 he is close to leaving the site. I see this commonly on several Andeg pages, especially those focused on lead conversion. Whenever I drag the mouse arrow to the browser close button, Popup appears.

  • I think it says something on this link: <a href="https://answall.com/questions/40982/display-algo-ao-tryingto close mysite"> https://answall.com/questions/40982/display-algo-ao-tryingto close mysite</a>

  • You may like this https://answall.com/questions/229319/evento-tirar-o-mouse-da-janela/229356#229356

1 answer

2

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 output occurs in the window::

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");
    }
});

Below is the full 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");
        }
    });
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.