<script type="text/javascript" src="/scripts/a.js"></script>
<script type="text/javascript" src="/scripts/b.js"></script>
I have two simple JS scripts, loaded in the above order. a.js has a reference to a function e.g. foo() in b.js i.e. a.js invokes foo() that is defined in b.js. On some servers, an error was thrown because foo() isn’t recognized when a.js is executed; on some other machines, no error was thrown.
Do those two scripts loaded simultaneously in parallel? Or, in sequence? Does the answer depend on the architecture of the servers (e.g. multi-core, etc.)?
Thanks.
Downloads may or may not be in parallel (depending on the browser), but parsing of the scripts are in the sequence they are laid on the page.
if you used that order (
a.jsbeforeb.js), withfoo()declared inb.jsbut called ina.js, this will result in an error becausefoo()was called before it existed.It’s the same reason why JS libraries encourage you to load their scripts before any user scripts. That way, their references exist before you use them.