Osc*_*son 28 javascript api adsense sizzle
我无法相信这有多难找到,但即使在Google开发者文档中我也找不到它.我需要能够动态,只有 JavaScript插入adsense.我也查看了StackOverflow,其他一些人已经问过这个但没有回复.希望这将是一个更好的解释,并会得到一些回复.
基本上,用户插入我的脚本,让我们调用它my.js(不能说它当前具体是什么.)my.js加载,并且在my.js他们的页面上显示某些嵌入式媒体然后我需要以某种方式附加生成的HTML:
<script type="text/javascript"><!--
google_ad_client = "ca-pub-xxx";
/* my.js example Ad */
google_ad_slot = "yyy";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
Run Code Online (Sandbox Code Playgroud)
在特定<div>(或其他)元素内.有任何想法吗?
PS没有像jQuery这样的库,我不能在页面上插入HTML,除非它是通过JavaScript而且必须插入到一个特定的<div>名字中(如果有帮助,我将使用Sizzle作为我的JS库)
Joh*_*ika 20
用于异步加载其他答案提议的AdSense脚本的简单技术将无效,因为Google使用document.write()AdSense脚本内部.document.write() 仅在页面创建期间有效,并且在异步加载的脚本执行时,页面创建将已完成.
要使其工作,您需要覆盖,document.write()以便在AdSense脚本调用它时,您可以自己操作DOM.这是一个例子:
<script>
window.google_ad_client = "123456789";
window.google_ad_slot = "123456789";
window.google_ad_width = 200;
window.google_ad_height = 200;
// container is where you want the ad to be inserted
var container = document.getElementById('ad_container');
var w = document.write;
document.write = function (content) {
container.innerHTML = content;
document.write = w;
};
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://pagead2.googlesyndication.com/pagead/show_ads.js';
document.body.appendChild(script);
</script>
Run Code Online (Sandbox Code Playgroud)
该示例首先将本机document.write()函数缓存在局部变量中.然后它覆盖document.write()并在其中,它用于innerHTML注入Google将发送到的HTML内容document.write().完成后,它将恢复本机document.write()功能.
这种技术来自这里:http://blog.figmentengine.com/2011/08/google-ads-async-asynchronous.html
小智 8
我的页面上已经有了adsense,但在我的博客文章中也向占位符注入了新广告.在我想要广告注入的地方,我添加了一个带有'adsense-inject'类的div,然后在文档准备就绪时运行此脚本,我知道adsense脚本已经为其他广告加载: -
$(document).ready(function()
{
$(".adsense-inject").each(function () {
$(this).append('<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3978524526870979" data-ad-slot="6927511035" data-ad-format="auto"></ins>');
(adsbygoogle = window.adsbygoogle || []).push({});
});
});
Run Code Online (Sandbox Code Playgroud)
如何将变量(google_ad_client 等)始终放在头部并动态附加其他部分,如下所示:
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://pagead2.googlesyndication.com/pagead/show_ads.js';
myDIV.appendChild(script);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20988 次 |
| 最近记录: |