stuff 1
stuff 2
stuff 3 with a link
Um Cookies 🍪 🍪 🍪 🍪 🍪
One way to visualize an HTML document is as a set of nested boxes.
html
, encloses the head
and the body
, which in turn, encloses other inner elements, such as headers and paragraphsIf you were tasked with writing a rendering engine for a browser, what kind of data structure would you use to represent an HTML document in your program? →
(maybe you're planning on building your own in Swift or Rust)
Let's look at another way of visualizing an HTML document →
Another way to view an HTML document is as a tree:
The DOM, or Document Object Model, is a standardized programming interface (an API) for representing and working / interacting with objects in HTML documents
Again, the DOM and the programming language are separate entities:
The DOM represents an HTML document as a group of nodes
As a point of comparison… there were built-in global objects that were accessible and specific to JavaScript in Node - what were some built-in objects that we used? →
console
global
require
functionprocess
, module
, etc.
The JavaScript engine in your browser also has access to a bunch of built in objects - and those objects let us access the DOM!
The DOM can be accessed by through a built-in global object called document
.
document
contains additional objects:
documentElement
- the root of the tree; represents the HTML element (the tags that enclose the entire document)body
- the body elementhead
- the head element
Let's see what these objects look like. →
Nodes can be categorized into types. What kind of node types do you think there are in an HTML document (let's check out the tree structure again)? →
Notice that the type of a node is specified by a constant. For example…
document.ELEMENT_NODE
document.TEXT_NODE
All nodes have a nodeType
property that specifies what kind of node it is.
Note that the objects we've seen so far, such as document
, document.body
, etc. are also nodes.
Consequently, they also have a nodeType property (compare with the constants →
Another two properties of Nodes are:
nodeName
- the name of the current node (read only)nodeValue
nodeValue
is null (see the docs)nodeValue
is the actual content of the nodeSpeaking JavaScript mentions that there are some shortcoming with the way the DOM interface is designed. For example:
childNodes
property of a node is not actually an array (let's see… it has length, but what methods would we expect from an array?→)
Fortunately, JavaScript allows us to create abstractions that smooth over these design flaws. There are many libraries available that make manipulating the DOM a breeze (JQuery being the most popular).
Of course, you can see that libraries, like JQuery, make things much easier for the programmer.
Some properties that you can use to move around the DOM are:
parentNode
- the node containing the current nodechildNodes
- an array-like object containing all of the current node's children
length
propertyfirstChild
- first child nodelastChild
- last child nodepreviousSibling
- the previous adjacent nodenextSibling
- the next adjacent node
And… let's see what these look like. →
Write a function called talksAbout
. Test on this markup.
talksAbout(document.body, 'schedule'); // returns true if the word 'schedule' is in body
document.ELEMENT_NODE
or document.TEXT_NODE
indexOf
to determine if the search string is a substring of the node's value (nodeValue
)indexOf
returns -1 if the substring is not found
function talksAbout(node, search) {
if (node.nodeType === document.ELEMENT_NODE) {
for (var i = 0; i < node.childNodes.length; i++) {
if (talksAbout(node.childNodes[i], search)) {
return true;
}
}
return false
} else if (node.nodeType === document.TEXT_NODE) {
return node.nodeValue.indexOf(search) > -1 {
}
}
All HTML elements can have a class and an ID attribute.
id="foo"
class="bar"
The following methods will give back an element or elements based on search criteria:
document.getElementById
- returns node with specified id attributedocument.getElementsByClassName
- returns nodes with specified class attribute (an HTML Collection)
document.getElementsByTagName
- returns nodes with specified tag name (an HTML Collection)
Changing attributes
// we can change certain attributes simply by using assignment... for example, id, src, or href
document.getElementById('foo').id = 'bar';
Changing text node values
// assuming we have an element_node, and we know its first child is a text element
node.firstChild.nodeValue = 'new text'
Assuming the following markup within a body tag, how would you change the href of the last link so that it takes you to an image search for cookies (using JavaScript)?
announcement