多个脚本标签与单个脚本标签

发布于 2024-11-16 22:23:02 字数 292 浏览 3 评论 0原文

使用嵌入代码的单个脚本标签或使用遍布整个 HTML 的相同代码的多个脚本标签之间是否有任何区别(性能、最佳实践等)?

例如:

<script>
    foo();
</script>
...
<script>
    bar();
</script>

与:

<script>
    foo();
    bar();
</script>

谢谢

Is there any difference (performance, best practices, etc) between using a single script tag with embedded code in it, or using multiple script tags with the same code spread all over the HTML?

For example:

<script>
    foo();
</script>
...
<script>
    bar();
</script>

versus:

<script>
    foo();
    bar();
</script>

Thanks

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

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

发布评论

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

评论(5

萌梦深 2024-11-23 22:23:02

对于您引用的内联脚本,不太可能有太多差异;然而,每次浏览器的 HTML 解析器遇到 script 标记时,它都会:

  • 突然停止
  • 在标记中构建一个文本字符串,直到第一次看到字符串 ""
  • 将文本交给 JavaScript 解释器,监听解释器在执行 document.write 时发送的输出
  • 等待解释器完成
  • 插入累积的内容输出接收到解析流中
  • 继续解析

因此,从理论上讲,增加此序列发生的次数可以增加页面加载时间。它还会影响解析器在令牌流中“向前看”的程度,这可能会降低效率。

所有这些听起来确实很引人注目,但您必须在您关心的各种浏览器中分析真实页面,以确定它是否具有现实世界的影响。

总之,尽可能合理地将它们结合起来。如果您无法合理地组合一对,请不要太担心,直到/除非您看到现实世界的问题。

以上是内联脚本。当然,如果您有多个 script 标记引用一堆外部 JavaScript 文件,那么您还会遇到必须下载每个文件的问题,并且发起 HTTP 请求是一件昂贵的事情(相对而言)因此最好将它们合并到一个文件中。

其他一些需要考虑的事项:

  • 在 HTML 中散布大量 script 标记可能会导致脚本维护变得困难
  • 将 HTML 和脚本分离到单独的文件中可以帮助您限制它们的程度耦合,再次帮助维护
  • 将脚本放在单独的文件中可以通过压缩器/压缩器/打包器运行该文件,最小化代码的大小并删除注释,从而使您可以自由地在源代码中进行注释,知道这些注释将被private
  • 将您的脚本放入外部文件中可以为您提供有机会按功能将内容分开,然后将它们组合成页面的单个文件(压缩/缩小/打包),以便有效地交付到浏览器

更多:

With inline script like what you quoted, there's unlikely to be much difference; however, every time the browser's HTML parser encounters a script tag, it:

  • Comes to a screeching halt
  • Builds up a string of the the text in the tag up until the first time it sees the string "</script>"
  • Hands that text off to the JavaScript interpreter, listening for output the interpreter sends it when you do a document.write
  • Waits for the interpreter to finish
  • Inserts the accumulated output received into the parsing stream
  • Continues its parsing

So increasing the number of times this sequence has to occur can, in theory, increase your page load time. It also affects the degree to which the parser can "look ahead" in the token stream, which may make it less efficient.

All of which sounds really dramatic, but you'd have to profile a real page in the various browsers you care about to determine whether it had a real-world impact.

So in summary, combine them as much as you reasonably can. If you can't reasonably combine a couple, don't worry about it too much until/unless you see a real-world problem.

The above is for inline script. Naturally, if you have several script tags referring to a bunch of external JavaScript files, you'll also have the issue that each of those files has to be downloaded, and initiating an HTTP request is an expensive thing (comparatively) and so it's best, in a big way, to combine those into a single file.

Some other things to consider:

  • Having lots of script tags scattered throughout your HTML may make it difficult to do maintenance on the script
  • Separating your HTML and script into separate files helps you limit the degree to which they're coupled, again aiding maintenance
  • Putting script in a separate file makes it possible to run that file through minifiers/compressors/packers, minimizing the size of your code and removing comments, thus leaving you free to comment in your source code knowing those comments will be private
  • Putting your scripts into external files gives you the opportunity to keep things separated by functionality, and then combine them into a single file for the page (compressed/minified/packed) for efficient delivery to the browser

More:

心舞飞扬 2024-11-23 22:23:02

我认为尽可能组合您的脚本会更好。某些浏览器在执行脚本块时必须暂停渲染。查看答案: Javascript 性能:多个脚本块与单个更大的块

Combining your scripts as much as possible is better in my opinion. Some browsers have to pause rendering while executing script blocks. Check out answer at: Javascript Performance: Multiple script blocks Vs single bigger block

才能让你更想念 2024-11-23 22:23:02

到目前为止,所有 JavaScript 代码都在一个标签中,但情况不一定如此。

您可以在文档中添加任意数量的标签。

遇到标签时就会对其进行处理。

希望这有帮助。

Up to this point all of the JavaScript Code was in one tag, this does not need to be the case.

You can have as many tags as you would like in a document.

The tags are processed as they are encountered.

Hope this helps.

左耳近心 2024-11-23 22:23:02

有些人认为,最佳实践是将所有脚本组合在单个脚本块或单个脚本文件中,仅加载真正需要的 JavaScript,并尽可能晚地加载它,以免减慢 html 的渲染速度。

除此之外,我确信使用单个脚本块比使用多个脚本块加载速度更快,因为它们必须单独评估。然而,这种差异可能无法被识别。

Some argue that it's best practice is to combine all scripts in a single script block or a single script file, load only the javascript that is really needed and load it as late as possible to not slow down the rendering of html.

Apart from that i am sure that using a single script block loads faster than using multiple script blocks since they have to be evaluated individually. However this difference might not be recognizable.

护你周全 2024-11-23 22:23:02

我使用多个标签。一个用于图像幻灯片,另一个用于文本幻灯片,因此我可以在同一个网页上同时显示两者 - 图像幻灯片和文本幻灯片。

I USE multiple tags. One for Slideshow of Images and another for Slideshow of Texts, so I can have both on the same web page - slideshow of images and slideshow of texts.

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