Docsity

JavaScript and The DOM, Slides of Technology

Partial preview of the text

Download JavaScript and The DOM and more Slides Technology in PDF only on Docsity!

JavaScript and the DOM

Lecture 6

Today’s Agenda

● Admin & Reminders ● Adding JavaScript to a webpage ● Where does the DOM come from?

From the console to a JS file

Now, we'll use these building blocks of a new programming language to control the

behavior of our pages (which we don't get with HTML/CSS!).

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

● document.getElementById returns the DOM object for an element with a given id

(note that you omit the # when giving an id)

● We'll learn about other ways to get DOM elements (e.g. document.querySelector to

get elements by class name) next lecture!

let name = document.getElementById("id");

What’s inside a DOM

For starters, the HTML attributes. This HTML above is a DOM object (let's call it puppyImg) with

these two properties:

● puppyImg.src -- set by the browser to images/puppy.png

● puppyImg.alt -- set by the browser to "A fantastic puppy photo"

**The **

Text inside of the button tag renders as the button text

To make a responsive button (or other UI controls):

1. Choose the control (e.g., button) and event (e.g., mouse click) of interest

2. Write a JavaScript function to run when the event occurs

3. Attach the function to the event on the control

Go!

Event Driven Programming

Unlike Java programs, JS programs have no main; they respond to user actions called events

Event-Driven Programming : writing programs driven by user events

Using a click Event Handler to Open the Box

Click me!

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*

1. Fetch the page

2. Parse the page

3. Build the page's DOM (and CSSOM)

4. Paint the page

*: As seen by Chrome.

Fetch Page

1. DNS

● www.google.com → 172.217.14.

2. HTTP GET

● GET /index.html HTTP/1.1, Host: www.example.com

● Collection of all this information: Request Headers

3. TCP/IP transfer

● Series of tubes...

4. Check: 200 OK?

● Collection of all this information: Response Headers