3
爲什麼我需要使用:爲什麼谷歌Analytics(分析)動態插入JavaScript到頁面
(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 async="true" type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>
我能看到的唯一原因是,他們有不同的子域爲SSL。如果不是,那麼是否有理由使用這種腳本注入技術?
主要點#2(和子域)。第一點可以通過[只是在沒有協議的情況下啓動URL]來解決(http://blog.niftysnippets.org/2011/01/skipping-protocol.html)(...'src =「// www.google -analytics.com/ga.js「'...)。 – 2013-02-11 15:43:47
@ T.J.Crowder - 是的,這也是正確的,但它是否適用於所有瀏覽器,包括舊版瀏覽器。 Bcoz GA必須與我想象的所有超級兼容。 – techfoobar 2013-02-11 15:44:41
此外,'location.protocol'可用於動態檢測正確的協議。 – marekful 2013-02-11 15:44:46