Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implications of multiple <script> tags in HTML

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

like image 697
Philip Avatar asked Jan 11 '11 15:01

Philip


People also ask

Is it OK to have multiple script tags in HTML?

Yes, we can write any number of tags inside tag. And browser access them in sequential order.

Can you have multiple script tags?

Multiple <SCRIPT> Tags Up to this point all of the JavaScript Code was in one <SCRIPT> tag, this does not need to be the case. You can have as many <SCRIPT></SCRIPT> tags as you would like in a document.

What do you think is the benefit of using multiple scripts?

An advantage of moving to separate script files is that you can re-use code on multiple pages. When you do that, it may be easier at build time to compress your scripts with YUICompressor or some other similar tool.

What is the purpose of different script tag?

Definition and Usage The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.


3 Answers

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!

like image 172
DVK Avatar answered Oct 29 '22 00:10

DVK


The fewer calls you make that instantiate a jQuery object, the less overhead you have -- but even if you are designing for old browsers running on 2nd generation hardware be wary of micro-optimizations. Profile your application and fix the parts that actually are the bottlenecks.

As for the way browsers handle multiple script tags -- it varies from browser to browser, from version to version, and sometimes even from operating system to operating system. All browsers execute each script tag in document order:

<script src="scripts/some_script.js"></script> <!-- Executed 1st -->
<script src="scripts/some_other_script.js"></script> <!-- Executed 2nd -->
<script>
// Some JavaScript
</script> <!-- Executed 3rd -->
<script>
// Some More JavaScript
</script> <!-- Executed 4th -->

However, other behaviors are not defined and there is variation. For example, Opera (at least on Windows XP for version 10.6) ran each script tag in its own context -- so local variables in the 3rd script block would be out of scope if referred to in the 4th script block.

<script>
var i = 42;
</script>
<script>
alert(i);
// Alerts "undefined" in Opera, but 42 in Firefox.
</script>
like image 30
Sean Vieira Avatar answered Oct 29 '22 00:10

Sean Vieira


The browser executes JavaScript sequentially (the same is true for jQuery since jQuery is just JavaScript).

As for having multiple script tags in HTML, there's no reason why this would be a problem. As Nabab asked, I would be interested in seeing your source for that.

like image 2
JasCav Avatar answered Oct 28 '22 22:10

JasCav