这个javascript与php一起工作,当用户试图离开页面时向用户显示广告(我知道的令人讨厌的功能......不是我的选择)。
用户尝试离开后所需的事件顺序为:
1. Onbeforeunload:显示广告 - The Ad before choice
2。一个。如果他们离开:没什么
b。如果它们停留:调用adDisplayed()在用户留下的数据库中标记。
3。如果他们点击广告,则另一个功能会标记他们点击了广告。
这适用于Chrome和IE,但在Firefox中,“2. a。”永远不会发生相反,“2. b。”无论他们是留下还是离开都会发生 onunload函数成功阻止adDisplayed()在离开时实际执行,但在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();
我已经在这方面工作了一段时间,我一直都在寻找答案,所以任何帮助都会很棒。谢谢!
答案 0 :(得分: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
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.";
}
}
缺点是它在你选择停留的时候并没有真正开火,但是如果你离开它就会开火,它仍然有效。
我会离开这个,以防它对其他人有帮助。