在你的html中有大量的脚本标记是否有任何性能问题? [英] Are there any performance issues in having a large number of script tags in your html?

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

问题描述

在html文件中有大量脚本标记(比如说50)是否存在问题?它是否会导致页面渲染时间的任何性能问题?

Are there any issues in having a large number (say 50) of script tags in an html file. Does it cause any performance issues in page rendering times?

我的页面上有一个节点,它将一个地理位置点放入一个数组中,以便在Google地图上使用。我可以为地理定位点创建一个单独的查询,但是我相信,如果我将其作为其他内容请求提取出来,效率会更高。

I have a section on my page that's pulling a geolocation point and putting this into an array for use on a google map. I could create a separate query for just the geolocation points but I believe it is more efficient if I pull this out as other content is requested.

<script type="text/javascript">points[{count}] = [{job_latitude},{job_longitude},'{title}'];</script>


推荐答案

当浏览器遇到 SCRIPT 元素,它停止解析和呈现HTML并开始分析和解释其内容。这是因为代码 SCRIPT 元素可能会改变已解析的DOM树。

When a browser encounters a SCRIPT elements it stops parsing and rendering the HTML and starts parsing and interpreting its contents. That is because the code SCRIPT elements could alter the already parsed DOM tree.

这就是为什么建议将 SCRIPT s放在底部文档,以便整个文档正文已被解析。

That’s the reason why it’s recommended to put SCRIPTs at the bottom of the document so that the whole document body is already parsed.

所以我宁愿要求地理位置(也许你可以在服务器端收集它们并在最后打印它们)。或者,如果你不能或不想这样做,请添加 defer 属性添加到您的 SCRIPT 元素中,以告诉解析器不要等待脚本。

So I would rather request the geo locations altogether (maybe you can collect them on the server side and print them at the end). Or if you cannot or don’t want to do that, add the defer attribute to your SCRIPT elements to tell the parser not to wait for the script.

这篇关于在你的html中有大量的脚本标记是否有任何性能问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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