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!