Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make sure a Javascript script is first to run?

I've noticed some scripts seem to be called before others on a certain page, I was wondering, what is the specific order for scripts to load? In-page before referenced .js scripts? Are they run in order from first <script> mentioned to last in page, or Is this browser-dependent? How can one make sure that a specific script is first to run in a page?

like image 656
NoBugs Avatar asked Feb 21 '12 23:02

NoBugs


2 Answers

As long as no scripts are dynamically loaded or marked as async or defer, scripts are run or evaluated in the order encountered in the page. So, the first scripts encountered run first.

An externally referenced script file that must be loaded will cause all further javascript execution to wait until that externally referenced file is loaded and parsed and runs.

So, the evaluation order of normal (non-async, non-defer) javascript is 100% determinate as the order it is encountered in the page.

like image 58
jfriend00 Avatar answered Nov 15 '22 21:11

jfriend00


I've noticed some scripts seem to be called before others on a certain page. I was wondering, what is the specific order for scripts to load?

This is set by W3C in their language specifications. For the HTML 4.01 Specification, for instance, it's defined in Section 18.2.4 Dynamic modification of documents, item 1.

In-page before referenced .js scripts?

See above. No, inline and linked scripts are handled identically.

Are they run in order from first mentioned to last in page, or is this browser-dependent?

The specifications call for them to be run sequentially from top to bottom. You'll have to find a browser that implements the language according to specification. I can't think of any right now that handle SCRIPT tags differently, but I'm sure that is possible.

Another thing to consider is the definition of "run." That may sound like semantic parsing, but it's not. JavaScript, like any programming language, is itself designed to behave according to standards. JavaScript is specified in the ECMA-262 5.1 Edition / June 2011 standard to evaluate from left-to-right in Section 7 Lexical Conventions. (Line endings are treated as the left-most character of the next line.) This document also provides conventions for the order in which statements and other operations are evaluated, such as WHILE or FOR statements.

How can one make sure that a specific script is first to run in a page?

(1) Put it at the top, and (2) choose a browser that implements the language specification.

However, I feel there may be something more behind this question. If you're trying to stop unexpected code from executing, you'll have to block it until the ONLOAD event handler registers that the page is complete. (Simply enclose your operations in a function or surround them with an IF to check for a boolean flag, i.e. isLoaded being set true by ONLOAD.) Then, when the ONLOAD event handler fires off, you can kick off operations on your own schedule without having to worry about things like uninstantiated DOM objects.

like image 31
ingyhere Avatar answered Nov 15 '22 20:11

ingyhere