双脚本标记中有任何值吗? [英] Any value in double script tags?

查看:25
本文介绍了双脚本标记中有任何值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在几段代码中看到了这一点,并且我不想假设"它并不重要,但这是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) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/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.

这篇关于双脚本标记中有任何值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆