0

This javascript works in conjunction with php to display an ad to a user when they are attempting to leave the page (obnoxious functionality I know.. Not my choice).

The desired order of events once the user tries leaving is:
1. Onbeforeunload: Display the ad - The Ad before choice
2. a. If they leave: Nothing
    b. If they stay: Call adDisplayed() to mark in the db that the user has stayed.
3. If they click on the ad, another function marks that they have clicked.

This is working in Chrome and IE, but in Firefox, "2. a." never happens. Instead, "2. b." happens regardless of if they stay or leave.
The onunload function is successfully preventing adDisplayed() from actually executing if they leave, except for in Firefox.

var markSeen = 0;

function regularChatDisplay(e) {
    //This is called just before the page unloads      
    if(validNavigation == false && windowInteraction == true){   
        isDone = true;  
        window.onbeforeunload = null;
        window.pagehide = null;  

        showAd();    
        AdHasDisplayed = 1; 

        //Setting this on a timout enables us to execute this only if the user stays                                
        markSeen  = setTimeout("adDisplayed();", 800);        
        return "Wait! Want to see other offers you may be interested in? Please stay to see an additional offer.";           
    }      
} 
function noTimeout() {   
    //This prevents adDisplayed() if the user leaves instead of staying when presented with the dialogue box 
    clearTimeout(markSeen);
}            

function setOnBeforeUnloadEvent() {                       
    if (!isDone) {                                    
        window.onbeforeunload = regularChatDisplay;    
        window.pagehide = regularChatDisplay;    
        window.onunload = noTimeout;
    }      
}
function adDisplayed(){
    //Placing something here will cause it to be executed after the user chooses to stay on the page after having been prompted  
    $("#seenSpan").load('pages/adSeen.php');     
}                         
setOnBeforeUnloadEvent();  

I've been working on this for a while now and I've looked all over for answers, so any help would be awesome. Thanks!

Lucas
  • 13
  • 5
  • `noTimeout` appears to be calling ` clearTimeout` with an undefined parameter. Does defining `markseen` outside of `regularChatDisplay` do anything? – traktor Feb 17 '16 at 21:26
  • Thanks, I just saw that. It doesn't help. Also fyi, the onunload function is definitely being called, but it seems like it isn't always being called earlier than the timeout is being executed. – Lucas Feb 17 '16 at 21:43

1 Answers1

0

I've found a workaround. It's not perfect, but it works:

function regularChatDisplay(e) {    
    //This is called just before the page unloads      
    if(validNavigation == false && windowInteraction == true){   
        isDone = true;  
        window.onbeforeunload = null;
        window.pagehide = null;  

        showAd();    
        AdHasDisplayed = 1; 

        //Setting this on a timout enables us to execute this only if the user stays
        if(browserType != "Firefox"){
            markSeen  = setTimeout("adDisplayed();", 800);  
        }
        else{  
            window.onbeforeunload = adDisplayed;    
        }                                                     
        return "Wait! Want to see other offers you may be interested in? Please stay to see an additional offer.";           
    }      
} 

The downside is that it doesn't actually fire at the time you've chosen to stay, but since it fires if you leave, it still works.

I'm going to leave this in case it's helpful to anyone else.

Lucas
  • 13
  • 5