【发布时间】:2011-12-07 19:44:59
【问题描述】:
我已经阅读了很多主题并尝试了很多东西,但我无法得到我想要的东西。 我刚刚将我的 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文件中了。 无论如何感谢您的帮助。
【问题讨论】:
-
你得到什么错误?
-
错误:adsense 未定义
-
这样做 --
window.load = function() { adsense(); }; -
@greengit 好多了,现在错误消失了但是页面上没有显示添加
-
从 adsense() 函数中删除
<!--和//-->也
标签: javascript load deferred