2011-12-05 284 views
0

我已經閱讀了很多主題,並嘗試了很多東西,但我無法得到我想要的東西。 我剛剛在頁面末尾移動了我的js代碼,現在我收到了一些錯誤。在加載頁面後加載javascript

這是我的網頁看起來像:

<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我有我的谷歌的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

我試過推遲=「推遲」,window.onload ...

有什麼想法? 感謝

我得到這個錯誤在Firebug: 錯誤:沒有定義的AdSense

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> 
中,如果我把下面的代碼fonctions.js

, 「確定」 顯示:

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代碼的頁面(調用下面 - 在你做警報(「這裏」);)這是很好的展示......所以我的AdSense代碼是否正確

更新: 我有終於改變了解決方案,我把代碼放入了一個.html文件,並且我使用php來包含它。所以它不再在我的js文件中。 無論如何感謝您的幫助。

+0

_你有什麼錯誤? – SLaks

+0

錯誤:未定義adsense – remyremy

+1

這樣做 - 'window.load = function(){adsense(); };' – treecoder

回答

3

window.onload需要函數回調;但是,您正在執行adsenseadsense(),並且adsense不會返回一個函數;因此,window.onload將放棄。更改爲:

window.onload = adsense; 

UPDATE

以上答案應該被丟棄,但我要離開它使人們可以知道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> 

UPDATE

我忘記了腳本延遲未在任何支持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> 
+0

我還是得到了與此相同的錯誤 – remyremy

+0

哦,哎呀,我看到的問題,我會更新我的答案:) – Ryan

+0

感謝您的幫助,但仍然無法正常工作。 如果我把警報(「OK」);在adsense()之後;它已顯示,但廣告仍未顯示。 但是,如果我在該腳本之前插入我的fonctions.js,則會顯示廣告... – remyremy

1

window.onload = adsense();調用adsense()立即並指定其返回值onload