简体   繁体   English

加载页面后加载javascript

[英]load javascript after loading the page

I have read a lot of topics and tried a lot of stuff but I can't get what I want. 我读了很多主题,尝试了很多东西,但我找不到想要的东西。 I just moved my js code at the end of the page and now I get some errors. 我只是在页面末尾移动了我的js代码,现在出现了一些错误。

This is how my page looks like: 这是我的页面的样子:

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

In fonctions.js I have my google adsense code: 在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;
    //-->
    }

The idea was to have the same code for adsense at only one place but I can't get it to load after the file fonctions.js 这个想法是只在一个地方有相同的adsense代码,但是在文件fonctions.js之后我无法加载它

I tried defer="defer", window.onload ... 我尝试了defer =“ defer”,window.onload ...

Any ideas? 有任何想法吗? Thanks 谢谢

I get this error in Firebug: Error : adsense is not defined 我在Firebug中收到此错误:错误:未定义adsense

PS: I would like to avoid to use Jquery (to avoid making pages too big) PS:我想避免使用Jquery(以避免使页面太大)

UPDATE: 更新:

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

in fonctions.js if I put the following code, the "ok" is displayed: 在fonctions.js中,如果我输入以下代码,则会显示“确定”:

function adsense(){
alert ("ok");
}

However if I have this code, the ad is not displayed: 但是,如果有此代码,则不会显示广告:

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

My guess is that it's a Google issue... The code cannot be loaded in this way...? 我的猜测是这是Google的问题...无法以这种方式加载代码...? If I put the adsense code in the page (below the call - where you do alert('here'); ) it is well displayed... So my adsense code is correct 如果我将adsense代码放在页面中(在呼叫下方-您在哪里进行提醒('here'); ),则会很好地显示...因此我的adsense代码正确

UPDATE: I have finally changed the solution, I've put the code in a .html file and I include it using php. 更新:我终于改变了解决方案,我将代码放在.html文件中,并使用php将其包括在内。 So it's not in my js file anymore. 所以现在不在我的js文件中。 Thanks for your help anyway. 还是要谢谢你的帮助。

window.onload expects a function callback; window.onload需要一个函数回调; however, you are executing adsense with adsense() , and adsense doesn't return a function; 但是,您正在使用adsense()执行adsense ,而adsense不会返回函数; therefore, window.onload will discard that. 因此, window.onload会丢弃它。 Change to: 改成:

    window.onload = adsense;

UPDATE UPDATE

The above answer should be discarded, but I'm leaving it up so that people can know that window.onload expects a function callback :) 上面的答案应该被丢弃,但是我将其保留,以便人们可以知道window.onload需要一个函数回调:)

Keep in mind that defer on the script element will instruct the browser to wait until the page has been loaded to execute the script; 请记住,对script元素的defer将指示浏览器等待页面加载后执行脚本; however, your fonctions.js is in the src attribute of your last script tag; 但是,您的fonctions.js位于最后一个script标签的src属性中; therefore, your deferred script will most likely execute before adsense has been defined, because the browser will make an http request to retrieve your script. 因此,您的延后脚本很可能会在定义adsense之前执行,因为浏览器会发出http请求来检索您的脚本。 This will allow deferred scripts to continue executing while adsense is not defined. 在未定义adsense情况下,这将允许延迟执行的脚本继续执行。 Try this in place of your original deferred script: 尝试使用此方法代替原始的延迟脚本:

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

UPDATE UPDATE

I forgot that script defer is not supported in anything outside of IE. 我忘记了IE之外的任何功能均不支持脚本延迟。 Therefore, the deferring issue should not be at play here; 因此,延期问题不应该在这里起作用。 however, I tested the following code in FF and Chrome, and it works: 但是,我在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(); calls adsense() immediately and assigns its return value to onload . 立即调用adsense()并将其返回值分配给onload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM