加载页面后加载javascript

load javascript after loading the page

本文关键字:加载 javascript      更新时间:2023-09-26

我已经读了很多主题,尝试了很多东西,但我不能得到我想要的。我只是移动了我的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>

在函数。js我有我的谷歌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在只有一个地方,但我不能让它加载后的文件函数。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>
如果我输入以下代码,就会显示"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代码放在页面(在call下面-在你做alert('here');的地方),它会很好地显示…所以我的adsense代码是正确的

更新:我终于改变了解决方案,我把代码在一个。html文件,我包括它使用php。所以它不在我的js文件中了。谢谢你的帮助。

window.onload期望函数回调;然而,你用adsense()执行adsense, adsense不返回函数;因此,window.onload将丢弃它。改变:

    window.onload = adsense;

更新

上面的答案应该被丢弃,但我留下它,以便人们可以知道window.onload期望一个函数回调:)

请记住,script元素上的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>

window.onload = adsense();立即调用adsense() ,并将其返回值赋给onload