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