简体   繁体   English

如何知道Google Adsense广告已加载JavaScript

[英]How to know that google adsense ad is loaded with JavaScript

I need to know that my google adsense ad (in div id="bottomAd") is loaded. 我需要知道我的Google adsense广告(在div id =“ bottomAd”中)已加载。

I tried to wait 5 sec, parse div, and get all "a": 我试图等待5秒,解析div,并得到所有的“ a”:

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

It's now working. 现在正在工作。

Adsense load with iframe that has their own body. AdSense具有自己的主体的iframe加载。 So that how to get all "a" element? 这样如何获取所有“ a”元素? Or other way to know that adsense ad is loaded? 还是通过其他方式知道adsense广告已加载?

I can't load iframe content because of private policy (prevented XSS attack). 由于私人政策(防止XSS攻击),我无法加载iframe内容。 So next code doesn't wokrs: 因此,下一个代码不会起作用:

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

    }
}

I have never used google adsense before but I suspect there will be some events you can subscribe to tell whether something has occur or not. 我以前从未使用过Google Adsense,但我怀疑会有一些事件可以订阅以告知是否已发生。

I did some research on your behalf and found this documentation to be particularly useful, I think the event you are looking for is the ADS_LOADED event. 我代表您进行了一些研究,发现此文档特别有用,我认为您要查找的事件是ADS_LOADED事件。 Just do a ctrl - f find on the link for 'ADS_LOADED' https://support.google.com/adsense/answer/1705827?hl=en 只需执行一个ctrl - f即可在“ ADS_LOADED”链接上找到https://support.google.com/adsense/answer/1705827?hl=zh_CN

Here is the sample code suggested by the doc. 这是文档建议的示例代码。

adsLoader.addEventListener(AdsLoadedEvent.ADS_LOADED, onAdsLoaded);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM