The purpose of this class is to introduce to the student:
- How a webpage is made up of objects (DOM)
- How JavaScript can be used to manipulate those objects (DOM manipulation)
- Commonly used browser defined functions and properties
FIRST HALF (12.00 - 13.30)
Is this your first lecture for this class? Please introduce yourself briefly:
- Job
- Education
- City
- Why you love programming (if you do)
The Document Object Model (DOM) is an object-oriented representation of a web page (HTML document). Every HTML element (ex. h1
, p
or img
) is corrected first and then converted into a JavaScript object by the browser, making it possible for us to use JavaScript to change the contents. Using JavaScript code we can access the DOM
through a global object called document
or window.document
.
Show the student the following HTML in the browser, and then refer to it in the browser console.
<!DOCTYPE html>
<html>
<head>
<title>My title</title>
</head>
<body>
<h1>My header</h1>
<p>This is a nice paragraph</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<a href="https://www.w3schools.com/js/pic_htmltree.gif">My link</a>
</body>
</html>
Notice how the DOM is structured in a tree-like manner. It goes from top (highest) to bottom (lowest) level. It's very much like a family tree: the highest level is the great-great-great-grandparent, while the lowest level is the grand-grand-grand-child.
- Create an HTML file including the structure of a basic webpage (including
!DOCTYPE
,html
,head
andbody
, 1h1
and 1p
) - Find out how to target the
head
,body
andh1
elements using the browser console - Present your solution and how you figured it out (Teacher chooses two people)
The DOM is created by the browser: it reads your HTML file and transforms the elements into objects. We use JavaScript to select these elements in order to change them.
As developers we can use code others have written. The browser contains predefined functions that we can use in order to get certain things done. For example, we can add/update/remove new HTML elements to the DOM. The browser also offers us properties, so that we can also play with the user's viewing experience. For example, we can modify the browser's width programmatically so we can offer a responsive website.
// 1. Print the current page
window.print();
// 2. Get the URL from the address bar
window.location.href;
// 3. Make a XHR request to an external service
window.fetch('https://dog.ceo/api/breeds/image/random');
Find browser functions or properties to show how we can...
- display an alert box?
- find out what the browser's name is?
- go back one page?
We can use by the browser predefined functions and properties to shape the user's experience of our application.
SECOND HALF (14.00 - 16.00)
DOM manipulation
refers to the act of using JavaScript to select and modify elements within the DOM. We do this in order to provide users interactivity with the page: for example, if a button is clicked the background color changes, or if a menu item is hovered over it becomes bigger.
<!DOCTYPE html>
<html>
<head>
<title>My title</title>
</head>
<body>
<h1>My header</h1>
<p>This is a nice paragraph</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<button>My button</button>
<a href="https://www.w3schools.com/js/pic_htmltree.gif">My link</a>
</body>
</html>
// 1. Make body background color red
const body = document.body;
body.style.background = 'red';
// 2. Change paragraph content
const p = document.querySelector('p');
p.innerHtml = 'This paragraph has changed!';
// 3. Create and add a new element to an existing HTML element
const thirdLi = document.createElement('li');
const ul = document.querySelector('ul');
ul.appendChild(thirdLi);
// 4. On button click alert the user!
const button = document.querySelector('button');
button.addEventListener('click', function() {
alert('You clicked the button!');
});
Write JavaScript code that...
- changes the
href
value tohttps://www.hackyourfuture.net/
- changes the
button
text toMake background colored!
- colors the
body
background to your favorite color, when the button is clicked
Present your solution and how you figured it out (Teacher chooses two people)
Using JavaScript we can select and modify DOM elements. In this way we can provide the user useful interactions with the webpages they're engaged in.