我需要知道我的Google adsense广告(在div id =“ bottomAd”中)已加载。

我试图等待5秒,解析div,并得到所有的“ a”:

function OnBodyLoad() {
    setTimeout(function () {
        var bottomAd = document.getElementById("bottomAd");
        var linkArray = bottomAd.getElementsByTagName("a");//it's always empty
    }, 5000);
}


现在正在工作。

AdSense具有自己的主体的iframe加载。这样如何获取所有“ a”元素?还是通过其他方式知道adsense广告已加载?

由于私人政策(防止XSS攻击),我无法加载iframe内容。因此,下一个代码不会起作用:

var array = new Array();
findIframeLinks(bottomAd, "a", array);


...

function findIframeLinks(element, returnElementTagName, array) {
    array.push(element.getElementsByTagName(returnElementTagName));
    var innerIframes = element.getElementsByTagName("iframe");
    for (var i = 0; i < innerIframes.length; i++) {
        if (innerIframes[i].contentDocument) {
            var body = innerIframes[i].contentDocument.getElementsByTagName('body')[0];
            findIframeLinks(body, returnElementTagName, array);
        }

    }
}

最佳答案

我以前从未使用过Google Adsense,但我怀疑会有一些事件可以订阅以告知是否已发生。

我代表您进行了一些研究,发现此文档特别有用,我认为您要查找的事件是ADS_LOADED事件。只需在“ ADS_LOADED”的链接上进行ctrl -f查找
https://support.google.com/adsense/answer/1705827?hl=en

这是文档建议的示例代码。

adsLoader.addEventListener(AdsLoadedEvent.ADS_LOADED, onAdsLoaded);

关于javascript - 如何知道Google Adsense广告已加载JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34375055/

10-11 13:42