mraid

AddEventListener Mraid


Hey i'm a bit late but i have some problem with my code..

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>

My EvetListener Always work while the loading but after it it wont work anymore ...

By the way i have a code working on IOS to block the orientation in landscape but it dont work on Android why ?

Thanks ! :)


Solution

  • I noticed few problems with your code

    1. Make sure you first line of code is <script src="mraid.js"></script>,most probably it is since you have not pasted the whole code within script tag.
    2. Second check if mraid is still loading,if so then listen to mraid "ready" event. And inside read event callback handler add you other listners or whatever you want to do mraid related.
    3. Third when you are addiing event listners and using certain callback methods, but while removing you are removing different callback handlers e.g. while adding if you add this listner mraid.addEventListener("orientationchange", Orientation);then while removing you shall call mraid.removeEventListener("orientationchange", Orientation); and notmraid.removeEventListener("orientationchange", mraidIsReady);
    4. Fourth, you don't need to call Orientation handler un-necessary on every state change, listen to only orientationchange
    5. Fifth, Not all mraid complaint SDK's support orientationchange, if your Ad SDK doesn't support that change event then listen to

      window.addEventListener('orientationchange',Orientation);

    Here is the code

    <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();