The Browser Context and the DOM
  1. For most purposes, certainly ours, the most important context for JavaScript is an HTML browser page.
    1. Yes, there are non-browser contexts, such as nodejs mentioned earlier.
    2. Yes, there are non-HTML web pages that can use JavaScript, notably PDF or XML.
    3. But we're not doing that.
  2. We have the standard JavaScript built-ins, like String and Array.
  3. And things related to the browser and the window.
    1. The window object, also named root, contains it all.
    2. References to roots of related windows, such a parent or child frames.
    3. Built-in functions, including for controlling the window.
    4. The location object. Current URL and some other things.
    5. The history object. Limited use in current JS for security reasons.
    6. The screen object. Display parameters.
    7. The document object, representing the HTML displayed on the screen.
      1. The document holds the Document Object Model (DOM), a tree representing the source HTML.
      2. Each tag is represented by an object descended from class Element, which becomes a node in the tree.
      3. The children of each element are the elements nested inside it in the HTML.
      4. The browser builds the DOM from the HTML when the page loads. Then JavaScript can access and modify the DOM.
      5. The browser always displays the current DOM contents, so this allows JavaScript to update the page.
    8. Since JavaScript can also interact with the user (mouse and keyboard), and with the network, this allows dynamic page update.
  4. Events
    1. Running JavaScript in a page is not run like running a program from a command or GUI click. That program starts, runs until it is done, then stops.
    2. The JavaScript contained in a web page does not run until something happens: an event.
    3. Particular code or functions are associated with particular events, and the code runs when the event occurs.
    4. Code can be assigned to events either from HTML, or from JavaScript.
    5. These days, the total number of event types is approaching the number of atoms in the sun. But they generally fall into a few categories.
      1. Browser operations: Page load or unload, window expose or resize, etc.
      2. User interaction: Typing, clicking, scrolling, selecting, etc., or equivalents.
      3. Network and other communication events.
      4. Time expires.