简体   繁体   English

嵌入外部js脚本-合理的限制?

[英]Embedding external js scripts - reasonable limits?

We have main layout page, and we have some external scripts, that loading after page loaded via ajax. 我们有主布局页面,还有一些外部脚本,这些脚本在通过ajax加载页面之后加载。 Some of them are really slow, cause they are opening socket.io connection. 其中一些确实很慢,因为它们正在打开socket.io连接。 This slow down whole page load. 这会减慢整个页面的加载速度。

I have some questions: 我有一些疑问:

  1. Is it ok, if external widget contains more than 5 scripts in body? 如果外部窗口小部件的主体中包含5个以上的脚本,可以吗? What reasonable limits we should have on count of scripts per service? 我们应该对每个服务的脚本数量有什么合理的限制?

  2. <script src="/path/to.js"></script> embedded in ajax loaded html - will make sync or async query to server? 嵌入到Ajax加载的html中的<script src="/path/to.js"></script> -将对服务器进行同步或异步查询?

  3. How to avoid browser block, during external script load? 在外部脚本加载期间如何避免浏览器阻塞?

  4. How to improve speed loading time, when we have a lot of external scripts? 当我们有很多外部脚本时,如何提高加载速度?

Update: Finally I use HeadJS library. 更新:最后我使用HeadJS库。

  1. while it is ok to load many different scripts, it is encouraged to cut down on the different HTTP calls, as most browsers allow for up to 2 concurrent connections per each domain, this means your code will load them 2 by 2, and will not continue to run until they are loaded. 虽然可以加载许多不同的脚本,但建议减少不同的HTTP调用,因为大多数浏览器每个域最多允许2个并发连接,这意味着您的代码将2加载2,而不会继续运行,直到它们被加载。 best practices are to minify, and concatenate all of your scripts into fewer files. 最佳做法是最小化所有脚本并将它们串联到更少的文件中。
  2. as far as i know regular script tags will usually load synchronously, slowing down even ajax loaded html. 据我所知,常规脚本标记通常会同步加载,甚至会降低ajax加载的html的速度。
  3. there are a few good proposals to avoid browser blocks. 有一些好的建议可以避免浏览器阻塞。 either load your scripts at the bottom of your body tag, to ensure parsing everything before working on the heavy scripting. 要么将脚本加载到body标签的底部,以确保在处理繁重的脚本之前先解析所有内容。 you can also use the defer, and async tags (the latter is HTML5), or try loading your scripts after the document sends it's "ready" event. 您还可以使用defer和async标记(后者是HTML5),或者在文档发送“就绪”事件后尝试加载脚本。
  4. less HTTP calls, postpone parsing of some JS to when needed, and defer+async load your javascript externals to prevent freezing the browser. 更少的HTTP调用,在需要时推迟某些JS的解析,以及defer + async加载您的JavaScript外部组件,以防止浏览器冻结。

a good read is this: http://www.sitepoint.com/a-detailed-breakdown-of-the-ltscriptgt-tag/ 很好的读法是: http : //www.sitepoint.com/a-detailed-breakdown-of-the-ltscriptgt-tag/

it is very thorough in explaining the history and evolution of the script tag itself, and has some of it's best practices. 它在解释脚本标签本身的历史和演变方面非常详尽,并具有一些最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM