【问题标题】:Moved adsense code to end but ads sometimes do not display on initial site load将 adsense 代码移至末尾,但广告有时不会在初始网站加载时显示
【发布时间】:2015-04-16 15:43:58
【问题描述】:

这是我用于网站的代码框架,用于将广告加载移动到网站底部。虽然几乎一切正常,但我发现在第一次加载页面时时不时地没有广告出现。我不确定是不是因为浏览器不知道如何自动重新加载完成的 DOM,但这很烦人,因为每次我点击刷新时,都会出现一个广告。我不确定它的谷歌是否未能提供广告,或者它的浏览器是否没有按预期处理我的代码。

    <!-- Website header here -->
    <div ID="ADW">ADVERTISEMENT</div>
    <div ID="AB"><!-- advertisement goes here after load --></div>
    <script type="text/javascript">
            // choose ad unit based on user's screen
            // size and format ad box to that size.
            var s="#####";
            var w=728;
            var h=90;
            if(window.innerWidth<500){
                    if(w>320){s="####";w=320;h=50}else{s="#####";w=234;h=60}
            }
            var AD=document.getElementById('AB');
            AD.style.height=h+'px';
            AD.style.width=w+'px';
    </script>
    <!-- Website content here -->
    <div ID="ADS">
            <!-- Ad will load in this DIV after webpage is loaded -->
            <script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" type="text/javascript" async></script>
            <ins ID="XX" class="adsbygoogle"></ins>
            <script type="text/javascript">
                    // loaderr is only set on pages where ad isn't supposed to run.
                    if (typeof(loaderr)=='undefined'){
                    (adsbygoogle=window.adsbygoogle||[]).push({params:{data_override_format:"true",data_page_url:"http://example.com/",google_ad_slot:s,google_ad_client:"#####",google_ad_width:w,google_ad_height:h}});
                    }
            </script>
            <script type="text/javascript">
                    var x=document.getElementById('XX'); //cram ad in
                    x.style.height=h+'px'; // I know sizes are equal but ad can fit
                    x.style.width=w+'px';  // and this was tested.
                    // give title of all adsense IFRAMES
                    // to meet accessibility guidelines
                    var xif=document.getElementsByTagName('IFRAME');
                    for(var i=0;i<xif.length;i++){
                    xif[i].title='Advertisement '+i;
                    }
            </script>
    </div>
    <script type="text/javascript">
            // Display the ad but 10% of the time it doesn't appear.
            if (typeof(AD) !== 'undefined'){
                    AD.appendChild(document.getElementById('ADS'));
            }
    </script>

为了排除这种可能性,我想知道一种方法来添加重绘功能,以使原本打算展示的广告真正展示出来。我不是要求在页面加载期间更改广告。我只是要求在不要求用户点击刷新的情况下显示广告。有没有违反 AdSense 政策的 JavaScript 解决方案?

我将 adsense 广告编号和发布商 ID 替换为 ##### 以防止其他人弄乱我的广告。

【问题讨论】:

    标签: javascript browser rendering repaint adsense


    【解决方案1】:

    第一个&lt;script&gt; 有一个async attribute which means that it will load asynchronously。看起来你有一个“竞争条件”,因为它只在某些时候有效。

    最快的解决方案: 将删除 async 属性,因为这将强制脚本同步加载(因此,只要您将脚本放在 @987654327 的底部@ 在您的 HTML 中,DOM 将在脚本运行时准备好)。

    另一个选项:要更明确地说明您希望脚本何时运行,您可以使用DOMContentLoaded or load events,具体取决于您希望脚本在 DOM 准备好后立即运行还是当页面上的所有其他内容完成加载时。例如,

    document.addEventListener('DOMContentLoaded', function() {
      // code to run after DOM has been loaded and parsed
      alert('DOM is ready!');
    });

    【讨论】:

    • 有没有人成功地使用 google 的 async 代码并删除了 async 属性来赚取收入?
    • async 属性与收入无关;如果您有多个脚本,它只允许您的脚本异步运行,但是如果您需要在脚本运行之前存在 DOM,那么您需要使用以下任一方式对其进行编程 1) &lt;body 底部的同步脚本或 2) 将代码放在 DOMContentReadyload 事件回调中。删除 async 属性是否解决了您的问题?
    • 有点效果,但我现在决定最好将广告代码移到更靠近顶部的位置。感谢您的帮助。
    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 2016-07-01
    • 1970-01-01
    相关资源
    最近更新 更多