添加事件监听器
AddEventListener 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 上不起作用,为什么?
谢谢! :)
我注意到您的代码几乎没有问题
- 确保您的第一行代码是
<script src="mraid.js"></script>
,很可能是因为您没有将整个代码粘贴到脚本标签中。
- 第二次检查 mraid 是否仍在加载,如果是则监听 mraid "ready" 事件。在读取事件回调处理程序中添加你其他的
监听器或任何您想做的与 mraid 相关的事情。
- 第三,当您添加事件监听器并使用某些回调方法时,但在删除时您正在删除不同的
回调处理程序,例如如果添加此侦听器,则添加
mraid.addEventListener("orientationchange", Orientation);
然后
删除你应该打电话
mraid.removeEventListener("orientationchange", Orientation);
而不是mraid.removeEventListener("orientationchange", mraidIsReady);
- 第四,您不需要在每次状态更改时都不必要地调用 Orientation 处理程序,只听 orientationchange
第五,不是所有的mraid complaint SDK都支持orientationchange,如果你的Ad SDK不支持那个change事件那就听
window.addEventListener('orientationchange',方向);
这是代码
<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();
嘿,我来晚了一点,但我的代码有问题..
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 上不起作用,为什么?
谢谢! :)
我注意到您的代码几乎没有问题
- 确保您的第一行代码是
<script src="mraid.js"></script>
,很可能是因为您没有将整个代码粘贴到脚本标签中。 - 第二次检查 mraid 是否仍在加载,如果是则监听 mraid "ready" 事件。在读取事件回调处理程序中添加你其他的 监听器或任何您想做的与 mraid 相关的事情。
- 第三,当您添加事件监听器并使用某些回调方法时,但在删除时您正在删除不同的
回调处理程序,例如如果添加此侦听器,则添加
mraid.addEventListener("orientationchange", Orientation);
然后 删除你应该打电话mraid.removeEventListener("orientationchange", Orientation);
而不是mraid.removeEventListener("orientationchange", mraidIsReady);
- 第四,您不需要在每次状态更改时都不必要地调用 Orientation 处理程序,只听 orientationchange
第五,不是所有的mraid complaint SDK都支持orientationchange,如果你的Ad SDK不支持那个change事件那就听
window.addEventListener('orientationchange',方向);
这是代码
<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();