javascript - 加载页面后加载javascript

标签 javascript load deferred

我已经阅读了很多主题并尝试了很多东西,但我无法得到我想要的。 我刚刚将 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;
}

我的猜测是这是 Google 的问题...代码无法以这种方式加载...? 如果我将 adsense 代码放在页面中(在调用下方 - 您执行 alert('here'); 的位置),它就会很好地显示...所以我的 adsense 代码是正确的

更新: 我终于改变了解决方案,我将代码放入 .html 文件中,并使用 php 包含它。所以它不再在我的js文件中了。 不管怎样,谢谢你的帮助。

最佳答案

window.onload 需要一个函数回调;但是,您使用 adsense() 执行 adsense,并且 adsense 不会返回函数;因此,window.onload 将丢弃它。更改为:

    window.onload = adsense;

更新

上面的答案应该被丢弃,但我将其保留,以便人们知道 window.onload 需要一个函数回调:)

请记住,脚本元素上的 defer 将指示浏览器等待页面加载后再执行脚本;但是,您的 fonctions.js 位于最后一个 script 标记的 src 属性中;因此,您的延迟脚本很可能会在定义 adsense 之前执行,因为浏览器将发出 http 请求来检索您的脚本。这将允许延迟脚本在 adsense 未定义时继续执行。尝试用这个代替原来的延迟脚本:

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

更新

我忘记了 IE 之外的任何东西都不支持脚本延迟。因此,延迟问题不应该在这里发挥作用;但是,我在 FF 和 Chrome 中测试了以下代码,它可以工作:

    <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 type="text/javascript">
        function adsense() {
            alert('here');
        }
    </script>

关于javascript - 加载页面后加载javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8390267/

相关文章:

for-loop - 为什么不执行延迟功能?

javascript - 如何使用 loadRecord 将嵌套模型加载到 Extjs 表单中

javascript如何将对象深度绑定(bind)到新的 'this'值

jQuery 如何将一些 json 记录加载到表单字段中?

python - twisted.protocols.ftp.FTPClient 和 Deferreds

javascript - Ajax并行发帖: Define timeout for each request and get only suceeded

javascript - 如何将index.html/php更改为index.myname?

javascript - 修改 id 元素内 html 标签的样式

javascript - jQuery load() 失败,但没有 JS 错误

jquery - 如何修改此代码以仅影响正常链接? (基本的 jQuery)