1

Will this code guarantee the objects.js being loaded prior to tests.js? Say I have objects in objects.js which will be used in tests.js. I just wanna make sure in all browsers objects.js loads before tests.js so that I dont get "Undefined function" run time errors, etc.

Or can you suggest a better approach?

<head>
    <script src="Scripts/objects.js"></script>
    <script src="Scripts/tests.js"></script>
</head>
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
C graphics
  • 7,308
  • 19
  • 83
  • 134
  • yes, it's almost always parsed in the order they appear, but in rare cases with weird servers (generally, such as with apache, this shouldn't happen), they send out different parts of the file before others (idk why), and if it sent out the second script first, the browser would probably parse that one first. – markasoftware May 06 '14 at 00:11
  • 1
    @Markasoftware The only reason an earlier script is not run first is because it doesn't exist. – Ja͢ck May 06 '14 at 00:12
  • ok. Just that I heard somebody say that somewhere else – markasoftware May 06 '14 at 00:12

3 Answers3

2

Scripts are executed synchronously, in the order you declare them, unless you use async attribute.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
2

Yes, that will guarantee that objects.js is run before tests.js.

If there are no special attributes on the script tags that affect the loading (such as async or defer), then inline script tags run in the order they appear in the file. That is guaranteed by the specification.

There is a complete description of script loading order including all the special cases of dynamically loaded scripts and defer and async here in this popular answer:

load and execute order of scripts

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Because in most browsers, code is run procedurally, yes, but I would just combine the two files to avoid performance issues

bren
  • 4,176
  • 3
  • 28
  • 43
  • There are no "scoping issues" reasons to combine these two files. There may be load performance to combine them, but not "scoping" reasons. – jfriend00 May 06 '14 at 00:12