Order issues in perhaps one or more of the following situations:
- When one of your scripts contains dependencies on another script.
If the script is in BODY and not in HEAD. . UPDATE: HEAD vs BODY does not seem to matter. Questions for the order. Period.- When you run code in a global namespace that requires a dependency on another script.
The best way to avoid these problems is to make sure that the code in the global namespace is inside the $(document).ready() wrapper. Code in the global namespace must be loaded in such a way that executable code is defined first.
Checking the JavaScript error console in Firebug or the Chrome debugger can tell you what is happening in the script and tell you what needs to be changed for your new setting.
An order does not matter at all whether functions are called based on events such as pageload, clicks, inserted or deleted nodes, etc. But if function calls are made outside of events in the global namespace, that is, when problems arise. Consider this code:
JS file: mySourceContainingEvilFunctionDef.js
function evilGlobalFunctionCall() { alert("I will cause problems because the HTML page is trying to call " + "me before it knows I exist... It doesn't know I exist, sniff :( "); }
HTML:
<script> evilGlobalFunctionCall(); </script> <script type="text/javascript" src="mySourceContainingEvilFunctionDef.js"></script> ...
In any case, the above tips will help prevent these problems.
As an additional note, you might think that there are certain advantages to the speed of using the asynchronous nature of the browser to draw resources. Web browsers can open up to 4 asynchronous connections at the same time, which means that it is entirely possible that your one massive script may take longer than the same script being broken into pieces ! There is also Yahoo Research , which shows that a combination of scripts gives a faster result, so the results vary from one situation to another.
Since this is a balance between the time taken to open and close several HTTP connections and the time taken to limit one connection instead of several asynchronous connections, you may need to do some testing at your end to see what works best in your situation . Perhaps the time taken to open all connections is compensated by the fact that the browser can load all scripts asynchronously and exceed the delay when opening / closing connections.
With that said, in most cases combining a script is likely to lead to a quick increase in speed and is considered best practice.
jmort253 Feb 14 2018-11-11T00: 00Z
source share