javascript - 自适应 adSense 广告 - 动态重新加载

标签 javascript mobile dynamic responsive-design adsense

我在响应式网站 ( http://goo.gl/asEovC ) 上运行了来自 labnol.org 的代码:

<div id="google-ads-1"></div>

<script type="text/javascript"> 

    ad = document.getElementById('google-ads-1');

    if (ad.getBoundingClientRect().width) {
        adWidth = ad.getBoundingClientRect().width; // for modern browsers 
    } else {
        adWidth = ad.offsetWidth; // for old IE 
    }

    /* Replace ca-pub-XXX with your AdSense Publisher ID */ 
    google_ad_client = "ca-pub-XXXXXXXXXXXXXXXX";

    /* Replace XXXXXXXXXN with the AdSense Ad Slot ID */
    if ( adWidth >= 728 )
      google_ad_size = ["728", "90"],  /* Leaderboard 728x90 */
      google_ad_slot = "XXXXXXXXX0";
    else if ( adWidth >= 468 )
      google_ad_size = ["468", "60"],  /* Banner (468 x 60) */
      google_ad_slot = "XXXXXXXXX1";
    else if ( adWidth >= 336 )
      google_ad_size = ["336", "280"], /* Large Rectangle (336 x 280) */
      google_ad_slot = "XXXXXXXXX2";
    else if ( adWidth >= 300 )
      google_ad_size = ["300", "250"], /* Medium Rectangle (300 x 250) */
      google_ad_slot = "XXXXXXXXX3";
    else if ( adWidth >= 250 )
      google_ad_size = ["250", "250"], /* Square (250 x 250) */
      google_ad_slot = "XXXXXXXXX4";
    else if ( adWidth >= 200 )
      google_ad_size = ["200", "200"], /* Small Square (200 x 200) */
      google_ad_slot = "XXXXXXXXX5";
    else if ( adWidth >= 180 )
      google_ad_size = ["180", "150"], /* Small Rectangle (180 x 150) */
      google_ad_slot = "XXXXXXXXX6";
    else
      google_ad_size = ["125", "125"], /* Button (125 x 125) */
      google_ad_slot = "XXXXXXXXX7";

    google_ad_width = google_ad_size[0];
    google_ad_height=google_ad_size[1];

</script>

<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

上面的代码无法解决调整大小的问题,如果调整浏览器的大小,广告将保持相同的大小,并且布局将被破坏。这在平板电脑和智能手机上的设备方向发生变化的情况下变得相关。我正在尝试找到一种解决方案,以便在尺寸(尺寸组)发生变化时重新加载广告。

最佳答案

Google 有新的响应式代码,显然可以在屏幕方向更改后处理布局更改:

https://support.google.com/adsense/answer/3213689?hl=en

Support for ad size changes after a screen orientation change. If your responsive page changes its layout following a device orientation change (e.g., when a tablet or phone goes from portrait to landscape), we’ll request and load a new ad of the correct dimensions to fit the new page layout.

Note: When we load a new ad following a device orientation change, we cache the original ad that was shown. If the device then returns to its previous orientation, we’ll show the original ad again rather than load another new ad. As the ad refresh behavior for screen orientation changes creates additional ad requests, you might notice a slight decrease in your RPM and CTR metrics. Please be assured though that there's no impact to your overall revenue.

关于javascript - 自适应 adSense 广告 - 动态重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20821965/

相关文章:

javascript - 有没有办法在手机 chrome 上创建 devtools 面板

jsf - 如何使用 PrimeFaces 实现递归菜单

javascript - Angular.js linq.js 过滤器无法正常工作的地方

php - 如何收集有关我的 PHP 应用程序错误的客户端信息?

java - 如果其他应用程序在设备上崩溃,Android 应用程序会监听

android - 如何将我的动态布局添加到 ListView?

javascript - 将两个相互关联的菜单从静态转换为动态

javascript - 重置 CSS3 动画

javascript - PanoJS 由于缺少未丢失的方法而失败

android - Android默认浏览器如何调试查看HTML和CSS源码?