谷歌异步加载执行js
It seems like every website these days uses Google Analytics. And why not? It's an outstanding tool that gives you any and every statistic you could ever want. The problem with Google Analytics is that it's been, well, slow loading at times. That's why I was so pumped when I saw Google was releasing an async version of their script.
如今,似乎每个网站都使用Google Analytics(分析)。 那么为何不? 这是一款出色的工具,可为您提供任何想要的统计信息。 Google Analytics(分析)的问题在于,有时加载速度一直很慢。 这就是为什么当我看到Google发布其脚本的异步版本时,我非常激动的原因。
Google Analytics JavaScript (Google Analytics JavaScript)
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-#######-#']); //your code here
_gaq.push(['_trackPageview']);
</script>
<script type="text/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';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
</script>
The one thing you'll notice is the async attribute being set to true. Obviously you'll want to use your own Analytics code within the top portion of the JavaScript. You'll also want to put this at the top of the BODY. What a difference the async version can make -- my website seems to load faster due to this upgrade!
您会注意到的一件事是async属性设置为true 。 显然,您希望在JavaScript的顶部使用自己的Google Analytics(分析)代码。 您还希望将其放在BODY的顶部。 异步版本可以带来什么不同-由于此升级,我的网站加载速度似乎更快!
谷歌异步加载执行js