Chrome is powered by V8 Javascript Engine. When a web page is requested, let say index.html
requested, roughly the execution flow is about as follows:
- The HTML document gets downloaded.
- The parsing of the HTML document starts.
- HTML Parsing reaches
<script src="angular.js" ...
angular.js
is downloaded and parsed.
- HTML parsing reaches
<script src="main.js" ...
main.js
is downloaded, parsed and run.
- Parsing of HTML document ends.
When the point 4 is reached, angular.js
source code is downloaded and parsed. At this stage, a global object angular
is created, jQuery Lite is loaded as angular.element
and all other angular related functions other stuffs attached with the angular
global object.
Now at point 6, your main.js
is downloaded and run. In your file you have the following line,
var $e = angular.element(document.getElementsByName("username")[0]);
$e.triggerHandler('input');
When the V8 JS Engine parses and runs the first line, it knows about angular
because angular.js
file is parsed and attached with windows as angular
. Once the engine knows that there is an object called angular
in window, then obviously it could access the element
attached with the angular
object.
Links to understand how the javascript is loaded and executed.
- SO link about load order and execution.
- Another one is here
- How do browsers execute javascript
Hope you could understand.