Browsers and Events - 17% Flashcards
addEventListener params
- Type - ex: ‘click’
- Listener - ex: JS Fn or object w/a handleEvent method
- Optional 3rd param can be either an options object, or useCapture boolean
addEventListener options obj
Options obj properties:
- Capture - boolean, see useCapture card
- Once - boolean, Indicates the handler should only be invoked once
- Passive - boolean, The invoked fn will never call preventDefault (even if specified in the fn)
- Signal - This is an AbortSignal - Listener gets removed when the given AbortSignal object’s abort method is called
addEventListener useCapture boolean
Will trigger event handlers on event targets beneath it on the DOM tree
Calls events in order from outside to inside
False by default
Ex: if the inner div is clicked, and the outer div has an event handler, the outer div’s handler function will be called, then the inner div’s handler fn
History API properties
- length - # of session history entries, includes current page
- scrollRestoration - Allows web apps to set default scroll restoration behavior on history navigation. Can be auto or manual.
- state - returns a value representing the state at the top of the history stack - this is a way to look at the state w/o having to wait for a popstate event
History API methods
back, forward, go, pushState, replaceState
go - Asynchronously loads a page from the session history, -1 for prev page or 1 for the next page. Has no effect if out of bounds. Calling w/o params or a value of 0 reloads the current page.
pushState - pushes the given data onto the session history stack with the specified title (and, if provided, URL) Note: title ignored by all browsers but Safari
replaceState - updates the most recent entry on the history stack to have the specified data, title, and, if provided, URL
popstate event
A popstate event is dispatched to the window each time the active history entry changes while the user navigates the session history. It changes the current history entry to that of the last page the user visited, OR if history.pushState or history.replaceState were used, that history entry is used instead.
event objects
These are objs automatically passed to event handlers when fired
event.target - this refers to the element that triggered the event (such as a button)
event.currentTarget refers to the element that the eventListener is on
If event listener is on the outer div, and the button was clicked, the event.target is the button and the event.currentTarget is the outer div
event bubbling
Default behavior of nested elements that have event handlers
Ex: button inside div inside body - if all 3 have event handlers, all 3 will be fired when the button is clicked (starting w/button, then div, then body)
use stopPropogation( ) method on the event object to stop the bubbling
event capture
Reverses the default order of Event Bubbling
ex: normally if button is clicked order is button, div, body - with event capture will be body, then div, then button.
See useCapture boolean card.
Node.js
JS runtime - use JS to build network and server-side applications.
Node.js events
Node’s event model is different from JS - relies on listeners to listen for events and emitters to emit events periodically - while this doesn’t sound very different, the code is very different.
Uses on( ) to register an event listener and once( ) to register an event listener that unregisters after running once.
DOM
Web API for web documents, represents the document as nodes and objects.
Can be used w/any scripting language (a script is a program run by a browser)
Most nodes are elements, but not all
DOM data types (some)
Document, node, element, nodeList, attribute, namedNodeMap
Node - every object in the document is a node
Element - objects that implement both the DOM Element interface and the Node interface
NodeList - array of elements
Attribute - nodes in the DOM, much like elements (rare)
NamedNodeMap - like a NodeList, except a Map
DOM Interfaces
Many DOM objects implement several different interfaces
Ex: HTML form element gets its ‘name’ property from HTMLFormElement interface and its ‘className’ property from HTMLElement interface, but both properties are in the form object.
Default actions
Actions built into the browser, such as key presses and mouse events. These can be prevented or customized by JS using event.preventDefault method.