Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does order of javascript import matter?

I have an aspx page and imports jQuery, jTemplate and Flexigrid

    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/flexigrid.pack.js" type="text/javascript"></script>
    <script src="Scripts/jquery-jtemplates.js" type="text/javascript"></script>
    <script src="Scripts/jquery.json-1.3.min.js" type="text/javascript"></script>

Does the order of import statements above matter?

like image 755
dance2die Avatar asked Mar 22 '12 17:03

dance2die


1 Answers

Yes, if a script uses anything defined by another script during its initial load. Each script is loaded and evaluated in order, synchronously. (The downloads may be in parallel if the browser wants, but they'll be evaluated in order unless the defer or async attributes were specified and are supported by the browser.)

So for instance, at a guess I'd say at least the last two scripts use the jQuery symbol defined by the first script, and so they must appear after it, or you'll see errors like ReferenceError: jQuery is not defined.

The order of unrelated scripts doesn't matter, but where they build on each other (as in this case), it does.

like image 182
T.J. Crowder Avatar answered Sep 22 '22 06:09

T.J. Crowder