Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firefox OnOnOnLoad之后OnOnOnLoad之前加载_Javascript_Onbeforeunload_Onunload - Fatal编程技术网

Javascript Firefox OnOnOnLoad之后OnOnOnLoad之前加载

Javascript Firefox OnOnOnLoad之后OnOnOnLoad之前加载,javascript,onbeforeunload,onunload,Javascript,Onbeforeunload,Onunload,当用户试图离开页面时,这个javascript与php一起向用户显示广告(我知道这是一个令人讨厌的功能,但不是我的选择) 一旦用户尝试离开,所需的事件顺序为: 1.Onbeforeunload:显示广告-2。A.如果他们离开:什么都没有。如果他们留下来:调用adDisplayed()在数据库中标记用户已经留下来。3。如果他们点击广告,另一个功能会标记他们已点击 这在Chrome和IE中是可行的,但在Firefox中,“2.a”永远不会发生。相反,“2.b.”不管他们是留下还是离开都会发生。 on

当用户试图离开页面时,这个javascript与php一起向用户显示广告(我知道这是一个令人讨厌的功能,但不是我的选择)

一旦用户尝试离开,所需的事件顺序为:
1.Onbeforeunload:显示广告-
2。A.如果他们离开:什么都没有。如果他们留下来:调用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();  

我已经为此工作了一段时间,我已经到处寻找答案,所以任何帮助都会很棒。谢谢

我找到了一个解决办法。这并不完美,但它很有效:

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.";           
    }      
} 
缺点是它不会在你选择留下来的时候发射,但是如果你离开它就会发射,它仍然有效


我将留下这个,以防它对其他人有帮助。

noTimeout
似乎正在使用未定义的参数调用'clearTimeout'。在
regularChatDisplay
之外定义
markseen
有什么作用吗?谢谢,我刚刚看到了。这没用。另外,仅供参考,onunload函数肯定正在被调用,但它似乎并不总是在执行超时之前被调用。