The order matters because the cluetip.js
file has dependencies on jquery.js
and the browser loads the scripts in the order it encounters them. When the browser attempts to load the cluetip.js
script it tries to make use of these dependencies. Since jQuery is not loaded the statements relying on jQuery fail.
On a side note, Javascript is an interpreted language, the scripts are never actually compiled. This is why you discover most of your errors at runtime when something fails, if it were compiled you would be alerted to errors prior to visiting your web page. When the browser encounters a script, it begins to execute it. Most times any real action is delayed until the page has been loaded, so the first few tasks mainly establish global variables/objects such as jQuery or registering jQuery plugins.
I assume the cluetip.js
plugin attempts to register with jQuery
however it cannot find it since it hasn't been loaded yet, furthering the point that the order matters.
As always the best reference for this is the actual specification.
One way to picture this is through a short example. Image we had a page with these scripts:
OurPage.html
<head>
<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
</head>
And the scripts:
script1.js
alert(msg); //msg is undefined
script2.js
var msg = "hello world";
The browser first loads script1.js
which attempts to alert the msg
variable, however the msg
variable is included in script2.js
which has not been loaded yet, therefore we are informed by the browser that msg
is undefined. This is what is happening in your example, just on a larger level.