AddEventListener Mraid

时间:2015-09-16 08:29:21

标签: mraid

嘿,我有点迟了但我的代码有问题..

function Orientation()
{


if ( (window.orientation == 0) || (window.orientation == 180) )//portrait
    {

        Paysage.style.visibility = "hidden";
        Portrait.style.visibility = "visible";
        mraid.removeEventListener("stateChange", mraidIsReady);
        mraid.removeEventListener("orientationchange", mraidIsReady);
        mraid.addEventListener("orientationchange", Orientation);  
        mraid.addEventListener("stateChange", Orientation);
        //var video = document.getElementById("video");
        //video.pause();

    }
if ( (window.orientation == 90) || (window.orientation == -90) )//paysages
  {
        Portrait.style.visibility = "hidden";
        Paysage.style.visibility = "visible";
        mraid.removeEventListener("stateChange", mraidIsReady);
        mraid.removeEventListener("orientationchange", mraidIsReady);
        mraid.addEventListener("orientationchange", Orientation);  
        mraid.addEventListener("stateChange", Orientation);
        //var video = document.getElementById("video");
        //video.play();
    //overlayObj.style.visibility = "";
    //var video = document.getElementById("video");
    //video.play(); 
  }
}

function doReadyCheck()
{   
    if (mraid.getState() == 'loading') 
    {   
        mraid.addEventListener("orientationchange", Orientation);  
        mraid.addEventListener("stateChange", Orientation);  
    } 
    else
    {   
        mraid.addEventListener("orientationchange", Orientation);  
        mraid.addEventListener("stateChange", Orientation);         
    }
}
doReadyCheck(); 
</script>

我的EvetListener在加载时总是工作但在它之后它不再工作了......

顺便说一句,我有一个代码在IOS上工作以阻止横向的方向,但它不适用于Android为什么?

谢谢! :)

1 个答案:

答案 0 :(得分:0)

我注意到您的代码存在一些问题

  1. 确保您的第一行代码是<script src="mraid.js"></script>,很可能是因为您没有在脚本代码中粘贴整个代码。
  2. 第二次检查mraid是否仍在加载,如果是,那么请听mraid&#34; ready&#34;事件。内部读取事件回调处理程序为您添加其他内容 列表管理员或任何你想要做的事情都与mraid相关。
  3. 第三,当您添加事件列表器并使用某些回调方法时,但在删除时您将删除不同的 回调处理程序,例如添加此列表器时添加 mraid.addEventListener("orientationchange", Orientation);然后 移除你应该打电话 mraid.removeEventListener("orientationchange", Orientation);而非mraid.removeEventListener("orientationchange", mraidIsReady);
  4. 第四,你不需要在每次状态变化时都不需要调用Orientation处理程序,只听取orientationchange
  5. 第五,如果您的广告SDK不支持该更改事件,那么并非所有人都会抱怨SDK的支持方向更改

    window.addEventListener(&#39; orientationchange&#39;,取向);

  6. 这是代码

    <script src="mraid.js"></script>
    function Orientation()
    {
    
    
        if ( (window.orientation == 0) || (window.orientation == 180) )//portrait
        {
    
            Paysage.style.visibility = "hidden";
            Portrait.style.visibility = "visible";
    
    
        }
        if ( (window.orientation == 90) || (window.orientation == -90) )//paysages
        {
            Portrait.style.visibility = "hidden";
            Paysage.style.visibility = "visible";        
        }
    }
    
    function doReadyCheck()
    {
        if (mraid.getState() == 'loading')
        {
            //Mraid is still loading so listen to ready state change
            mraid.addEventListener("ready", mraidIsReady);
    
        }
        else
        {
            //Mraid is already ready so do your mraid related stuff here
    
            //orientationchange event will only be added in case your SDK supports    orientationchange otherwise add
            //window.addEventListener("orientationchange", Orientation);
            mraid.addEventListener("orientationchange", Orientation);
            //This is not needed,why do you need to listen to stateChange as well, but its upto you if you want to do that
            //mraid.addEventListener("stateChange", Orientation);
        }
    }
    /**
    * Mraid is ready, so add your mraid related code here
    */
    function mraidIsReady(){
        //Remove the ready listener
        mraid.removeEventListener("ready", mraidIsReady);
    
        //Now add mraid related listeners
        //orientationchange even will only be added in case your SDK supports orientationchange otherwise add       //window.addEventListener("orientationchange", Orientation);
        mraid.addEventListener("orientationchange", Orientation);
    
        //I don't know if you really need to do that this is un-necessary, so   every stateChange will trigger orientation check
        //mraid.addEventListener("stateChange", Orientation);
    }
    doReadyCheck();