Search through more than a hundred articles on every aspect of User.com

Alina Shafikova
Written by Alina Shafikova

Exit pop-up form

Show your users a special form for when they decide to leave your website


There is one type of form that can be described separately from the others: the exit po-pup form. For information on the other types of forms, read this article.

Before you start

You must have FTP/SFTP credentials to edit your website files, you need almost no knowledge of JavaScript, and you need 2 minutes to complete this process.

How to create an exit pop-up form

Copy the code below and paste it into the tags <script></script>.

function addEvent( obj, evt, fn ) {
 if ( obj.addEventListener ) {
   obj.addEventListener( evt, fn, false );
 }
 else if ( obj.attachEvent ) {
   obj.attachEvent( "on" + evt, fn );
 }
}
addEvent( document, 'mouseout', function( evt ) {
 if ( !!window.sessionStorage ) {
   var timeout = JSON.parse( window.sessionStorage.getItem( '__ue__timeout' ) );
   if ( !!timeout ) {
     var elapsedTime = ( new Date() - timeout ) / ( 1000 * 60 );
     if ( elapsedTime <= 1 ) {
       return
     }
   }
 }
 if ( evt.toElement == null && evt.relatedTarget == null ) {
   userengage( 'event.left_the_window', { 'mouseX': evt.clientX, 'mouseY': evt.clientY } );
   !!window.sessionStorage && window.sessionStorage.setItem( '__ue__timeout', JSON.stringify( + new Date() ) );
 }
} );


Now, let's go through few lines and see what we've got.

Line 14

Number "1" stands for the amount of minutes. So, in this example, the event will fire once every minute if the user tries to leave your website. Changing it to "5" will make this function fire event once every 5 minutes.

if ( elapsedTime <= 1 ) {

Line 20

left_the_window - stands for the name of your event. You can change it if you prefer to While mouseX and mouseY. This will determine at which points X and Y, the user tried to leave your website.

userengage( 'event.left_the_window', { 'mouseX': evt.clientX, 'mouseY': evt.clientY } );

 

Categories: