有人能解释一下下面的 JavaScript 是做什么的吗?

发布于 2024-12-02 13:40:36 字数 535 浏览 2 评论 0 原文

这是在网站的 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。我在一个网站上看到了这个,我想知道它的作用......

This was inside the script tag of a website.

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);
})();

I'm not very conversant with Javascript or jQuery. I saw this on a website and I'm wondering what it does...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌上芳菲 2024-12-09 13:40:36

您帖子中的代码是 Google 的分析跟踪代码。它不是 jQuery,它是纯 JavaScript。谷歌很好地解释了他们的跟踪代码的作用。

Google 文档资源

引自 Google 文档:

一般来说,Google Analytics 跟踪代码 (GATC) 会检索网络
页面数据如下:

  1. 浏览器请求包含跟踪代码的网页。
  2. 创建了一个名为 _gaq 的 JavaScript 数组,并执行了跟踪命令
    推入数组。
  3. 创建一个元素并启用异步加载
    (在后台加载)。
  4. 使用适当的协议获取 ga.js 跟踪代码
    自动检测到。获取并加载代码后,
    执行 the_gaq 数组上的命令并转换数组
    进入跟踪对象。后续跟踪电话将直接拨打
    谷歌分析。
  5. 将脚本元素加载到 DOM。
  6. 跟踪代码收集数据后,GIF 请求将发送至
    用于记录和后处理的分析数据库。

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:

In general, the Google Analytics Tracking Code (GATC) retrieves web
page data as follows:

  1. A browser requests a web page that contains the tracking code.
  2. A JavaScript Array named _gaq is created and tracking commands are
    pushed onto the array.
  3. A element is created and enabled for asynchronous loading
    (loading in the background).
  4. The ga.js tracking code is fetched, with the appropriate protocol
    automatically detected. Once the code is fetched and loaded, the
    commands on the_gaq array are executed and the array is transformed
    into a tracking object. Subsequent tracking calls are made directly to
    Google Analytics.
  5. Loads the script element to the DOM.
  6. After the tracking code collects data, the GIF request is sent to
    the Analytics database for logging and post-processing.
黄昏下泛黄的笔记 2024-12-09 13:40:36

这会动态插入一个脚本标记,然后加载并执行 Google Analytics 脚本。 Google Analytics 通常由网站所有者安装,以便收集网站的网络使用统计信息(页面点击量、用户查看模式、浏览器类型等)。

这样做的主要原因是脚本是通过与页面相同的协议(http/https)加载的,并且是异步加载的,因此页面内容的加载不会等待该脚本加载。它按照自己的时间加载并在加载时执行。页面可以在加载完成之前显示。如果在普通脚本标记中指定它,则页面必须等待其完成加载才能继续。

  1. 前三个留置权设置并初始化一个全局变量 _gaq,然后脚本将使用其中的数据。
  2. 然后,创建一个脚本元素。
  3. 它的类型设置为“text/javascript”。
  4. .async 属性设置为 true。
  5. .src 属性设置为使用页面使用的相同 http 或 https 协议。
  6. script 标签被插入到页面的 head 元素中。

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.

  1. The first three liens set up and initialize a global variable _gaq which the script will then use the data from.
  2. Then, a script element is created.
  3. It's type is set to "text/javascript".
  4. The .async property is set to true.
  5. The .src property is set to use the same http or https protocol that the page uses.
  6. The script tag is inserted into the head element of the page.
青朷 2024-12-09 13:40:36

它通过创建脚本元素并将其放置在文档中来加载谷歌分析 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

晨曦÷微暖 2024-12-09 13:40:36

这是谷歌分析。它允许网站查看统计信息,例如您在该页面上停留的时间、您去了哪里、您来自哪里......

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

淡墨 2024-12-09 13:40:36

这是谷歌分析代码,但它正在创建一个脚本标记,并将其源设置为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文