2011-12-05 20 views
0

私は多くの話題を読んで、たくさんのものを試しましたが、私が望むものを得ることができません。 私はちょうどページの最後に私のjsコードを移動し、今いくつかのエラーが発生します。ページをロードした後にjavascriptをロードする

これは私のページはどのように見えるかです:私は私のGoogle AdSenseのコードを持って

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

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

アイデアが一つだけでアドセンスのために同じコードを持っていることでした私はそれをファイルの後にロードすることはできません.js

私はdefer = "defer"、window.onloadを試しました...

アイデア? おかげ

私はFirebugの中にこのエラーが表示されます。 エラー:アドセンスが定義されていない

PS:

:私は

UPDATE(大きすぎてページを避けるために)のjQueryを使用するために避けたいですfonctions.jsで

<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"); 
} 
を私はこのコードを持っている場合

はしかし、広告は表示されません。私の推測では、それは、Googleの問題だということです

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コードが

UPDATE正しいです; - 私は(あなたが警告(「ここ」)を行うコールの下)ページにAdSenseコードを配置する場合最終的にソリューションを変更しました。コードを.htmlファイルに入れて、PHPを使用してインクルードします。だから私のjsファイルにはもうない。とにかく助けてくれてありがとう。

+0

_エラーはどうなりますか? – SLaks

+0

エラー:adsenseが定義されていません – remyremy

+1

これを行う - 'window.load = function(){adsense(); }; ' – treecoder

答えて

3

window.onloadは、関数コールバックが必要です。ただし、をadsense()と実行していて、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にその戻り値を割り当てます。

関連する問題