Browser Event Handling

Junghoo Cho

cho@cs.ucla.edu

Event Object

Event Handler

Event Bubbling

Single-Thread Execution

JavaScript Execution Timeline in Browser

  1. document object is created and document.readyState is set to loading
  2. Browser downloads and parses the page. Scripts are downloaded and executed synchronously in the order they appear in the page (if no async)
    • async script starts to be downloaded asynchronously in the background and gets executed as soon as they are available
  3. Once the page is completely parsed, document.readyState is set to interactive
  4. Browser fires DOMContentLoaded event and calls document.onload callback
  5. document.readyState is set to complete
  6. Browser waits for events and calls appropriate event handlers

Execution Timeline Example

<html>
<head><title>JavaScript Example</title></head>
<body>Click on this document!</body>
<script>
    let colors = [ "yellow", "blue", "red" ];
    let i=0;
    function ChangeColor(event) { 
        document.body.style.backgroundColor = colors[i++%3]; 
    }
    document.body.addEventListener("click", ChangeColor);
</script>
</html>

Notes on JavaScript Execution

window: Global Object (1)

window: Global Object (2)

References