



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























Today’s Agenda
● Admin & Reminders ● Adding JavaScript to a webpage ● Where does the DOM come from?
From the console to a JS file
Activity
Our Toolbox So Far: HTML/CSS + JS
Image source (MDN) and recommended reading
So far, we have used VSCode (or a similar editor) to write HTML/CSS for the structure/styles for our page.
We will continue to use VSCode to write JavaScript and add behavior.
As always, the webpage is rendered by the browser. We will add a link to our JS from HTML (similar to the
idea of linking a CSS file) and the browser will execute the JS after the HTML/CSS has been loaded.
**Linking to a JS file:
// example
Accessing an element by id
let name = document.getElementById("id");
What’s inside a DOM
**The **
Go!
Event Driven Programming
Using a click Event Handler to Open the Box
let boxBtn = document.getElementById("box-btn"); boxBtn.addEventListener("click", openBox);
function openBox() { let box = document.getElementById("mystery-box"); // 1. Get the box image box.src = "star.png"; // 2. Change the box image's src attribute! }
QUESTION: What's the difference? Event vs. Function call
In Java we have function calls, like:
public static void functionA () { // do some things }
functionA (); // call functionA
In JavaScript we have function calls, too:
function functionA () { // do some things }
functionA (); // call functionA
But JavaScript ALSO has event handling:
function eventHandler () { // handle an event }
button. addEventListener ("click", eventHandler );
Lifecycle of a browser loading a page*
Fetch Page