script 标签可以出现在 title 和 meta 标签之前吗?

Can script tag appear before title and meta tags?

我正在研究如何将 Google 分析跟踪代码嵌入到我的网页中。根据 https://developers.google.com/analytics/devguides/collection/analyticsjs/ 的文档,它说 "The code should be added near the top of the tag and before any other script or CSS tags",在这种情况下,代码是否可以放在 title 和 meta 标签之前,像这样:

    <HEAD>

    <!-- 2019-03-01: Google Analytics Tracking Code(async) for xxxx, based on Google online help -->
    <script>
        window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
      ga('create', 'UA-xxxx', 'auto', {'allowLinker': true});
      ga('require', 'linker');
      ga('linker:autoLink', ['shareit.com', 'mycommerce.com'] );    
      ga('send', 'pageview');
    </script>
    <script async src='https://www.google-analytics.com/analytics.js'></script>

    <TITLE>xxxxxx</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <META name="DESCRIPTION" content="xxxxxx">

这看起来有点奇怪。所以我只是想知道将脚本标签放在标题和元标签之前是否可以?

谢谢

不会有不良影响,但也不会有所改善。 GA 代码应该放在 CSS 和脚本标签之前,因为下载它们可能会阻止浏览器并延迟下载 Google 脚本,并且(更重要的是)延迟 运行 ga 命令 queue。元标记对处理并没有真正的影响,所以它们是否出现在代码之前并不重要,但浏览器并不真正挑剔,因为 header 标题和元中的位置出现了。

您面临的唯一限制是 <meta charset 必须是 must be serialized completely within the first 1024 bytes of the document

将脚本元素放在它之前可能会违反该约束。

据我所知,您可以在 head 标签之前使用脚本标签,如果您想优先执行脚本,请在 head 标签开始的下方使用它。