动态的Adsense插入使用JavaScript [英] Dynamic Adsense Insertion With JavaScript

查看:201
本文介绍了动态的Adsense插入使用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能相信这是多么难找,但即使在谷歌开发者文档,我不能找到它。我需要能够动态,的只有的使用JavaScript上插入AdSense。我也看了关于计算器和其他一些人都问这个,但没有任何反应。希望这将是一个更好的解释,并会得到一些回复。

I can't believe how hard this is to find, but even in the Google developer docs I can't find it. I need to be able to dynamically, only with JavaScript insert adsense. I also looked on StackOverflow and some others have asked this but no response. Hopefully this will be a better explanation and will get some replies.

基本上,用户插入我的剧本,让我们把它叫做 my.js (不能说具体是此刻的东西。) my.js 加载和 my.js 显示一些嵌入媒体的网页,然后我需要以某种方式生成的HTML从追加:

Basically, a user inserts my script, lets call it my.js (can't say what it is specifically at the moment.) my.js is loaded and in my.js some embedded media is displayed on their page then I need somehow to append the generated HTML from:

<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>

具体内幕&LT; D​​IV&GT; (或其他)元素。任何想法?

Inside a specific <div> (or whatever) element. Any ideas?

P.S。像jQuery没有图书馆,我不能插入HTML到页面上,除非它是通过JavaScript和它必须被插入到特定的&LT; D​​IV&GT; 我命名(I'用间我的JS库灒是否有帮助)

P.S. No libraries like jQuery, and I can't insert HTML onto the page unless it's through JavaScript and it has to be inserted into a specific <div> i named (I'm using Sizzle for my JS library if that helps)

推荐答案

用于异步加载其他的答案提出的AdSense的脚本将无法工作,因为谷歌使用的document.write()在AdSense脚本内的简单的技术。 document.write()的创建仅在页面的作品,并通过异步加载脚本执行的时候,网页制作将已经完成。

The simple technique used to asynchronously load the AdSense script proposed by other answers won't work because Google uses document.write() inside of the AdSense script. document.write() only works during page creation, and by the time the asynchronously loaded script executes, page creation will have already completed.

要完成这项工作,你需要覆盖的document.write(),所以当AdSense的脚本调用它,你可以操纵DOM自己。这里有一个例子:

To make this work, you'll need to overwrite document.write() so when the AdSense script calls it, you can manipulate the DOM yourself. Here's an example:

<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 = 'http://pagead2.googlesyndication.com/pagead/show_ads.js';
document.body.appendChild(script);
</script>

此示例首先缓存在本地变量本地的document.write()函数。然后,它覆盖的document.write(),并在它的内部,它使用的innerHTML注入HTML内容,谷歌将发送使用document.write()。一旦这样做了,它恢复了原生的document.write()函数。

The example first caches the native document.write() function in a local variable. Then it overwrites document.write() and inside of it, it uses innerHTML to inject the HTML content that Google will send to document.write(). Once that's done, it restores the native document.write() function.

该技术是从这里借来的:<一href=\"http://blog.figmentengine.com/2011/08/google-ads-async-asynchronous.html\">http://blog.figmentengine.com/2011/08/google-ads-async-asynchronous.html

This technique was borrowed from here: http://blog.figmentengine.com/2011/08/google-ads-async-asynchronous.html

这篇关于动态的Adsense插入使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆