我在我的网站上使用了响应式Google广告,但不幸的是,它一直无法正常工作,大部分时间都在返回
TagError:adsbygoogle.push()错误:availableWidth = 0没有插槽大小
我尝试通过定义广告尺寸来解决问题,但问题仍未得到解决.
.adslot_1 { width: 320px; height: 100px; }
@media (min-width:500px) { .adslot_1 { width: 468px; height: 60px; } }
@media (min-width:800px) { .adslot_1 { width: 728px; height: 90px; } }
@media only screen and (min-width:800px)and (max-width:3000px) {
#topbanner {
width: 640px;
height: 90px;
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,这是我使用的实际Google Adsense Google代码(出于安全考虑,我删除了pub-id和ad-slot.)
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- articles(auto) -->
<ins class="adsbygoogle adslot_1"
style="display:inline-block;"
data-ad-client="ca-pub-[MY_AD_ID]"
data-ad-slot="AD_SLOT_NOS"
data-ad-format="rectangle, horizontal"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
Run Code Online (Sandbox Code Playgroud)
Sor*_*n C 19
我用了:
window.onload = function() {
(adsbygoogle = window.adsbygoogle || []).push({});
}
Run Code Online (Sandbox Code Playgroud)
我的网站以特殊方式加载页面,大量使用AJAX,因此必须采用这种方式.希望这有助于某人.
EDIT 2018:我觉得我现在应该提一下,首选的方法是使用window.addEventListener( 'load', ... ),而不是旧的方法window.onload = ...
Ale*_*lex 13
在我们的例子中,我们在显示两个横幅时遇到问题,但仅限于Chrome.第一个横幅不会显示,它会产生错误:
availableWidth = 0没有插槽大小
第二个横幅将显示OK.问题不是在Safari或Firefox中发生的.试图通过CSS解决问题没有帮助.
我们通过改变:解决了这个问题:
(adsbygoogle = window.adsbygoogle || []).push({})
Run Code Online (Sandbox Code Playgroud)
至
$(document).ready(function(){(adsbygoogle = window.adsbygoogle || []).push({})})
Run Code Online (Sandbox Code Playgroud)
上面的方法需要jQuery.
小智 9
你需要删除其他不可见的,我如何使用(jQuery):
$(document).ready(function(){
var $analyticsOff = $('.adsbygoogle:hidden');
var $analyticsOn = $('.adsbygoogle:visible');
$analyticsOff.each(function() {
$(this).remove();
});
$analyticsOn.each(function() {
(adsbygoogle = window.adsbygoogle || []).push({});
});
});
Run Code Online (Sandbox Code Playgroud)
1.推送广告时确保DOM已准备就绪.
香草(仅限现代浏览器)
document.addEventListener("DOMContentLoaded", function() {
(adsbygoogle = window.adsbygoogle || []).push({});
});
Run Code Online (Sandbox Code Playgroud)
在jQuery中
$(document).ready(function(){
(adsbygoogle = window.adsbygoogle || []).push({});
});
Run Code Online (Sandbox Code Playgroud)
在Angular
$timeout(function () {
(adsbygoogle = window.adsbygoogle || []).push({});
});
Run Code Online (Sandbox Code Playgroud)
2.确保页面上的广告数量正确无误.
adsbygoogle数量与您在网页上投放的广告数量相对应.3.对于响应单元,请添加宽度和高度.
自适应广告单元需要在ins元素上指定的宽度和高度.你可以这样做:
ins {
min-width: 300px;
min-height: 50px;
}
Run Code Online (Sandbox Code Playgroud)
我使用setTimeout解决了它,因为问题是GA代码没有在默认加载时选择父级的大小.
setTimeout(function(){(adsbygoogle = window.adsbygoogle || []).push({})}, 1000);