有没有办法让新的 Adsense 异步代码与 Rails 4 应用中的 Turbolinks 一起可靠地工作?
这是直接来自 google 的示例 adsense 异步代码:
<script async src="http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
data-ad-slot="6440411535"></ins>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
所有广告在全页刷新时都能正常加载,但当我随机浏览网站时,它们无法可靠加载(有时没有一个加载,有时只加载一两个)。
我想解决方案是以某种方式重新加载 page:change
事件上的广告。
我还想知道是否可以使用 adsense 通过 Turbolinks 进行“基于 ajax”的页面重新加载。
最佳答案
此代码目前可以很好地与 Turbolinks 5 配合使用。
function parseAds() {
var ads = document.querySelectorAll('.adsbygoogle');
ads.forEach(function(ad) {
(adsbygoogle = window.adsbygoogle || []).push({});
});
}
document.addEventListener('turbolinks:load', parseAds);
关于javascript - Turbolinks 与 Adsense 异步广告代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19938174/