JavaScript Web APIs
Updated:
What is a Web API?
- Web API: Application programming interface for the Web
- Provides communication or interaction between software applications, and enables web sites and client applications to have access to certain data
- We can make use of a large number of Web APIs available!
- Typically used with JavaScript (not always the case)
I did some research about the following four interfaces since they are frequently used!
1. EventTarget
- Implemented by any object that can receive events and may have listeners on them
- Any target of events implements the three methods:
EventTarget.addEventListener()
,EventTarget.removeEventListener()
: Registers and removes an event handler of a specific event typeEventTarget.dispatchEvent()
: sends anEvent
to the object, invoking the affectedEventListener
s in the appropriate order
- An example of creating and triggering events by Mozilla
2. Node
Node
inherits fromEventTarget
- An abstract base class, the primary datatype for the entire Document Object Model
- A generic name for any type of object in the DOM hierarchy
- Many other DOM API objects are based upon
Node
- Objects such as
Document
,Element
, andDocumentFragment
are its subclasses, which implement the functionality ofNode
. - Every kind of DOM node is represented by an interface based on
Node
.
- Objects such as
- There is no such thing as a
Node
object! - A text node is a
Node
, but not anElement
: see editing aTextNode
object.
3-1. Element
Element
inherits fromNode
:Element
“is-a”Node
object- The most general base class from which all element objects (i.e. objects that represent elements) in a
Document
inherit- HTMLElement interface is the base interface of HTML elements
- A specific type of node that can be directly specified in the HTML file with a tag
- Defines properties and methods such as
id
,class
,innerHTML
, etc.
3-2. Document
Document
also inherits fromNode
:Document
“is-a”Node
objectDocument
interface for HTML documents also also inherits from theHTMLDocument
interface- An interface that represents any web page loaded in the browser
- Serves as an entry point into the DOM (Document Object Model) tree
- DOM tree: A kind of tree whose nodes represent an HTML or XML document’s contents
- For example, the following document
<html>
<head>
<title>My Document</title>
</head>
<body>
<h1>Header</h1>
<p>Paragraph</p>
</body>
</html>
-
has a DOM tree that looks like this:
- A DOM tree preserves all whitespace!
- Describes the common properties and methods for any kind of document (i.e.
HTMLDocument
implementsDocument
)
Leave a comment