简体   繁体   English

为什么Google会覆盖他们的Google Analytics跟踪代码?

[英]Why does Google unescape their Analytics tracking code?

Just getting off my JavaScript training wheels. 刚下载我的JavaScript训练轮。

Why does Google choose to unescape the document.write line in Part 1 below? 为什么谷歌选择unescapedocument.write的低于1线?

Why don't they just write it like this? 他们为什么不这样写呢? Maybe unescape is required for some older browser compatibility? 某些较旧的浏览器兼容性可能需要unescape吗?

document.write('<script src="'
    + gaJsHost
    + 'google-analytics.com/ga.js" type="text/javascript"></script>');

For reference, the entire Google Analytics tracking code looks like this: 作为参考,整个Google Analytics跟踪代码如下所示:

Part 1: 第1部分:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol)
    ? "https://ssl."
    : "http://www."
);
document.write(unescape("%3Cscript src='"
    + gaJsHost
    + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"
));
</script>

Part 2: 第2部分:

<script type="text/javascript">
try
{
    var pageTracker = _gat._getTracker("UA-0000000-0");
    pageTracker._trackPageview();
}
catch(err){}
</script>

I understand what the rest of the code does, just curious about the unescape part. 我理解其余代码的作用,只是对unescape部分感到好奇。

Edit 编辑

The bottom line is, unescape is required. 底线是, unescape是必需的。 Voted to close this question because it is a duplicate (see answer marked correct). 投票结束这个问题因为它是重复的(见答案标记正确)。

It means the code will work in XML / XHTML and HTML without having to mess with CDATA 这意味着代码将以XML / XHTML和HTML工作,而不必弄乱CDATA

Please see: https://stackoverflow.com/questions/1224670/what-is-the-advantage-of-using-unescape-on-document-write-to-load-javascript 请参阅: https//stackoverflow.com/questions/1224670/what-is-the-advantage-of-using-unescape-on-document-write-to-load-javascript

My understanding is when </script > is found even inside the quotes "</script>" the parser wrongly understood that, its reach end of the script, so they cannot do like "</script>" 我的理解是,当发现</script >甚至在引号"</script>" ,解析器错误地理解了它,它到达脚本的末尾,所以它们不能像"</script>"

And Google wants to make sure variables like pageTracker are set before the google-analytics.com/*.js load, so unescaping %3Cscript and %3E%3C/script%3E is only the way for them. 谷歌希望确保在google-analytics.com/*.js加载之前设置像pageTracker这样的变量,因此unescaping %3Cscript%3E%3C/script%3E只是他们的方式。

just my 2 cents, sorry If I say wrong. 只是我的2美分,对不起如果我说错了。

Writing directly into the document without using the '<' or '>' characters means that you don't have to escape them in document formats which interpret these literally. 直接在不使用“<”或“>”字符的情况下直接写入文档意味着您不必以文档格式转义它们,这些格式可以解释这些字体。 Otherwise, the correct interpretation is that the <script> tags begin inside of the string, which is not what's desired. 否则,正确的解释是<script>标签在字符串内部开始,这不是所希望的。

Also, note that there's an error in your proposed alternative code (you missed a quote mark after the end of the src attribute). 另请注意,您提出的替代代码中存在错误(您在src属性结束后错过了引号)。

I think that: 我觉得:

document.wrIte('<script src="'"

will fail HTML Validation as well. 也将失败HTML验证。 Interestingly, it also breaks the Preview on this comment box :) 有趣的是,它也打破了这个评论框上的预览:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM