我已经阅读了很多主题并尝试了很多东西,但我无法得到我想要的东西。我只是在页面末尾移动了我的 js 代码,现在我得到了一些错误。
这是我的页面的样子:
<html>
<head>
bla bla
</head>
<body>
bla bla
<div class="advertising">
<script type="text/javascript" defer="defer">
window.onload = adsense();
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<script language="javascript" type="text/javascript" src="fonctions.js"></script>
</body>
</html>
在 fonctions.js 我有我的 google adsense 代码:
function adsense(){
<!--
google_ad_client = "pub-xxxxx";
/* 120x600, date de création 11/06/11 */
google_ad_slot = "xxxxx";
google_ad_width = 120;
google_ad_height = 600;
//-->
}
想法是只在一个地方为 adsense 提供相同的代码,但我无法在文件 fonctions.js 之后加载它
我试过 defer="defer", window.onload ...
有任何想法吗?谢谢
我在 Firebug 中收到此错误:错误:未定义 adsense
PS:我想避免使用Jquery(避免使页面太大)
更新:
<script type="text/javascript" defer="defer">
(function() { // 'sandbox' javascript pattern to prevent clobbering
// global namespace
var executeProxy = function() {
if (typeof adsense === 'function') { // adsense is configured
adsense();
} else { // adsense is not configured;
// therefore, try again later
setTimeout(executeProxy, 50);
}
};
executeProxy();
}());
</script>
<script language="javascript" type="text/javascript" src="fonctions.js"></script>
在 fonctions.js 中,如果我输入以下代码,则会显示“ok”:
function adsense(){
alert ("ok");
}
但是,如果我有此代码,则不会显示广告:
function adsense(){
google_ad_client = "pub-xx";
/* 120x600, date de création 16/04/11 */
google_ad_slot = "xxx";
google_ad_width = 120;
google_ad_height = 600;
}
我的猜测是这是谷歌的问题......无法以这种方式加载代码......?如果我将 adsense 代码放在页面中(在调用下方 - 你在哪里执行alert('here');)它会很好地显示......所以我的 adsense 代码是正确的
更新:我终于改变了解决方案,我将代码放在一个 .html 文件中,并使用 php 包含它。所以它不再在我的js文件中了。还是要谢谢你的帮助。