我已经在几段代码中看到了这一点,并且我不想假设"它并不重要,但这是Google Analytics(分析)代码的副本:
I've seen this in a few pieces of code and I didn't want to "assume" it was unimportant but this is a copy of the Google Analytics code:
<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "ssl." : "www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-xxxxxx"); pageTracker._trackPageview(); } catch(err) {} </script>您会注意到有两个打开/关闭脚本标签.有什么理由将代码位封装在两个不同的脚本标签中是有好处的?我的第一个反应就是简单地删除冗余.
You'll notice there are two open / close script tags. Is there any reason why encapsulating the code bits in two different script tags is beneficial? My first reaction would be simply to remove the redundancy.
推荐答案第一块将< script> 标记写入页面.我认为,如果代码全部放在一个块中,则不能保证在执行第二部分代码之前会先加载写入的< script> .
The first block writes a <script> tag to the page. I think if the code was all in one block there would be no guarantee the written <script> would be loaded before the second part of the code executed.
通过使用两个块,写入的< script> 将在第二个块执行之前加载(包含 _gat 对象).
By using two blocks, the written <script> will load (which contains the _gat object) before the second block executes.
更多推荐
双脚本标记中有任何值吗?
发布评论