多个< script>的含义HTML中的标签 [英] Implications of multiple <script> tags in HTML

查看:152
本文介绍了多个< script>的含义HTML中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过,建议不要在您的HTML中多次实例化jQuery。这对我来说很合理,但是:
不是Javascript单线程吗?将jQuery留在后面,浏览器如何执行这些多个脚本标记?平行或一个接一个?



谢谢,菲利普

解决方案

简单的回答:

在一个简单的场景中(标签是原始HTML文本的一部分),浏览器必然会一个接一个地执行它们。详细讨论不同的注意事项

JavaScript不一定是单线程的(它取决于你的JavaScript引擎的实现,例如参见 Web Workers )。



但是,单个< script> 标签是按顺序执行的。

作为参考,请参阅 JavaScript:权威指南。引用章节12.3。执行JavaScript程序:


出现在和标签之间的JavaScript语句按照外观顺序执行;当一个文件中出现多个脚本时,脚本将按照它们出现的顺序执行。如果脚本调用document.write(),那么传递给该方法的任何文本都将在结束标记后立即插入文档中,并在脚本完成运行时由HTML分析器进行分析。同样的规则适用于包含具有src属性的单独文件的脚本。






请注意,上述情况仅适用于代码的直线上升在标签中。但是,订单可能会受到以下影响:


  • setTimeout()来电(duh)


  • defer属性

  • / strong>,请注意,通过< script src =xxxx/> 外部加载的JavaScript代码仍然会按顺序执行,但是,很可能浏览器会<并行下载代码 - 取决于浏览器的实现(但仍按照正确的顺序安排下载的代码片段的执行)。

    这个警告对于你想要有一些奇怪的黑客很重要,而JavaScript源代码的URL实际上是一个CGI脚本,它执行某些操作,而你试图依赖正确的脚本下载顺序。



    同样,它不会影响您的浏览器JS引擎对这些脚本的执行顺序。 b
    $ b

    然而,更重要的警告是,如果您真的将< script> 标签附加到根据 appendChild() call) 12/22 / loading-javascript-files-in-parallel.aspxrel =noreferrer>本帖子,以及 MSDN博客文章基于,非IE浏览器不要保证执行顺序!取决于哪个标签的代码先完成下载!


    I have read that it is not recommended to instantiate jQuery multiple times in your HTML. This makes perfect sense to me, but: Isn't Javascript single-threaded anyway? And leaving jQuery behind, how does the browser execute these multiple script tags? In parallel or one after another?

    Thanks, Philip

    解决方案

    Simple answer:

    In a simple scenario (tags are part of original HTML text), the browser definitely executes them one after another.

    Detailed discussion with different caveats

    JavaScript isn't necessarily single-threaded (it depends on the implementation of your JavaScript engine, e.g. see Web Workers).

    BUT, the individual <script> tags are executed sequentially.

    For reference, please see JavaScript: The Definitive Guide. Quoting Chapter "12.3. Execution of JavaScript Programs":

    JavaScript statements that appear between and tags are executed in order of appearance; when more than one script appears in a file, the scripts are executed in the order in which they appear. If a script calls document.write( ), any text passed to that method is inserted into the document immediately after the closing tag and is parsed by the HTML parser when the script finishes running. The same rules apply to scripts included from separate files with the src attribute.


    Please note that the above is only true of "straight up" execution of code in tags. The order can, however, be affected by:

    • setTimeout() calls (duh)

    • defer attribute

    • Dynamic attachement of the <script> tags - see the last section of this answer.


    As a caveat, please note that JavaScript code loaded externally via <script src="xxxx" /> would still be executed sequentially, BUT, it is quite possible that the browser would DOWNLOAD the code in parallel - depends on browser implementation (but still schedule the execution of downloaded code snippets in correct order).

    This caveat is important in case you want to have some weird hack whereas the URL for the JavaScript source is actually a CGI script which does something and you try to depend on the correct order of downloads for the logic in the script.

    Again, it would have no bearing on your browser JS engine's execution order of those script pieces.


    However, a far more important caveat is that if you actually attach the <script> tags with external sources dynamically (e.g. via appendChild() call), according to this SO post, as well as the MSDN blog the post was based on, non-IE browsers do NOT guarantee the order of execution! It will depend on which tag's code finished downloading first!

    这篇关于多个&lt; script&gt;的含义HTML中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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