2011-06-20 17 views
1

私は現在、次があります。jQueryとGoogleアナリティクスの読み込みを組み合わせる方法はありますか?

<script src="http://www.google.com/jsapi?key=..." type="text/javascript"></script> 
<script> 
    //<![CDATA[ 
    google.load('jquery', '1.6'); 
    //]]> 
</script> 


<script type="text/javascript"> 
     var _gaq = _gaq || []; 
     _gaq.push(['_setAccount', '...']); 
     _gaq.push(['_trackPageview']); 

     (function() { 
      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
     })(); 
</script> 

はJSをロードするには、2つの非常に異なった方法のように思えます。これらを組み合わせて負荷が最も効率的な方法がいくつかありますか?

答えて

0

この呼び出しでは、最初にgoogle jsapiを読み込む必要があります。

google.load('jquery', '1.6'); 

あなただけasyncronouslyロードjqueryのは、以下を使用し、他のスクリプトファイルの呼び出しに平行する場合:

var jq = document.createElement(“script”); 
jq.src = “/path/to/jquery.js”; jq.type=”text/javascript”; jq.async=true; 
document.getElementsByTagName(“head”)[0].appendChild(jq); 
+0

感謝を。一つの質問。私はasync = trueが新しいと思う。それはすべてのブラウザーで使用できますか? – Michael

+0

https://developer.mozilla.org/En/HTML/Element/Script#Attributes – Acorn

+0

@Michaelそれは新しいブラウザでサポートされています。これをチェックしてくださいhttp://stackoverflow.com/questions/1834077/which-browsers-support -script-async-async –

関連する問題