有人能解释一下下面的 JavaScript 是做什么的吗?
这是在网站的 script
标签内。
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18914337-1']);
_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);
})();
我不太熟悉 Javascript 或 jQuery。我在一个网站上看到了这个,我想知道它的作用......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您帖子中的代码是 Google 的分析跟踪代码。它不是 jQuery,它是纯 JavaScript。谷歌很好地解释了他们的跟踪代码的作用。
Google 文档资源
引自 Google 文档:
The code in your post is Google's analytic tracking code. It's not jQuery, it's pure JavaScript. Google does a good job explaining what their tracking code does.
Google Docs Resources
Quote from Google's docs:
这会动态插入一个脚本标记,然后加载并执行 Google Analytics 脚本。 Google Analytics 通常由网站所有者安装,以便收集网站的网络使用统计信息(页面点击量、用户查看模式、浏览器类型等)。
这样做的主要原因是脚本是通过与页面相同的协议(http/https)加载的,并且是异步加载的,因此页面内容的加载不会等待该脚本加载。它按照自己的时间加载并在加载时执行。页面可以在加载完成之前显示。如果在普通脚本标记中指定它,则页面必须等待其完成加载才能继续。
This dynamically inserts a script tag which then loads and executes a Google Analytics script. Google Analytics is typically installed by a site owner in order to gather web usage statistics for the site (page hits, user viewing patterns, browser types, etc...).
The main reason for doing it this way is that the script is loaded over the same protocol (http/https) as the the page is and it's loaded asychronously and therefore the loading of the page content does not wait for this script to load. It loads on it's own time and executes when it's loaded. The page can display before it is done loading. If it was specified in a normal script tag, the page would have to wait for this to finish loading before continuing.
_gaq
which the script will then use the data from.它通过创建脚本元素并将其放置在文档中来加载谷歌分析 JavaScript 代码。
您可以在此处查看文档:
http://www.google.com/support/googleanalytics/bin/answer.py?answer=55488&utm_source=DiscoverList&utm_medium=et&utm_campaign=en_us&hl=zh-CN
It's loading the google analytics javascript code by creating a script element and placing it in the document.
You can see the documentation here:
http://www.google.com/support/googleanalytics/bin/answer.py?answer=55488&utm_source=DiscoverList&utm_medium=et&utm_campaign=en_us&hl=en
这是谷歌分析。它允许网站查看统计信息,例如您在该页面上停留的时间、您去了哪里、您来自哪里......
It's google analytics. It allows the website to view stats, like how long you have been on the page, where you went to, where you came from...
这是谷歌分析代码,但它正在创建一个脚本标记,并将其源设置为 google-analytics.com/ga.js
That is google analytics code, but it's creating a script tag, , and setting the source of that to google-analytics.com/ga.js