在网页中插入 Google AdSense 广告,一般将源代码直接嵌入主题模板页面。事实上 Google AdSense 支持并允许使用 JS 文件调用。当然前提是不要因任何原因修改代码,或手动影响广告的定位。这会因违反计划政策而被 Google 咔嚓账号。
芒果以 468×60 的经典广告尺寸代码为例,说明 JS 文件调用的方法。
获得广告单元代码如下:
<script type="text/javascript"><!-- google_ad_client = "发布商 ID"; /* 468x60, 创建于 xx-xx-xx */ google_ad_slot = "xxxxxxxxxx"; google_ad_width = 468; google_ad_height = 60; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
得到这段代码后,需要将其转化为 JS 形式:
document.writeln("<script type=\"text\/javascript\"><!--"); document.writeln("google_ad_client = \"发布商 ID\";"); document.writeln("\/* 468x60, 创建于 xx-xx-xx *\/"); document.writeln("google_ad_slot = \"xxxxxxxxxx\";"); document.writeln("google_ad_width = 468;"); document.writeln("google_ad_height = 60;"); document.writeln("\/\/-->"); document.writeln("<\/script>"); document.writeln("<script type=\"text\/javascript\""); document.writeln("src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">"); document.writeln("<\/script>")
鉴于手工更改比较麻烦,推荐使用在线 HTML 转 JS 工具,例如:http://www.hao123.com/haoserver/jstohtm.htm
将转换后的代码保存为 JS 文件,调用方法如下:
<script language="JavaScript" src="ggad_468x60.js" type="text/javascript"></script>
当然,针对强大的 WordPress 程序,可以使用各种 AdSense 广告管理插件来管理代码。此外,该方法不局限于 AdSense,其他广告商同样适用。
版权所有,转载请注明出处。
转载自 <a href="https://mangguo.org/use-js-file-call-google-adsense-ad/" title="使用 JS 文件调用 Google AdSense 广告" rel="bookmark">使用 JS 文件调用 Google AdSense 广告 | 芒果小站</a>
上一篇 在线巴巴,即时掌握网站运行情况
下一篇 点击日志标题跳转到外部链接的方法
如果喜欢这篇文章,欢迎订阅芒果小站以获得最新内容。
/2009-09-02 13:37还好,就是每个广告都要导入,其实和直接插入没什么区别!
我认为在 wp 中,你将 adsense 的代码转 js 的话,还不如将 adsense 代码存放在而外的文件中,用 include() 或 require() 函数引入呢!
/2009-09-02 14:31我的 GG 广告申请了几次都还没有通过。
/2011-01-12 16:40不错,受教了。。。。。
/2012-05-02 22:13博主能提供一个对联广告代码吗?
/2012-05-19 18:30能不能做那个浮动的广告啊。
/2012-10-15 17:02你都不知道,JS调用很好!但是容易被人故意刷! 所以做好设置外站不能调用!呵呵!
/2013-01-15 21:18博主,你的阴影广告是咋弄的?
/2013-01-15 23:37http://mangguo.org/google-adsense-advertisement-shadow-effect/