lecture06
lecture06
Lecture 6
Today’s Agenda
● 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
JS/HTML Connection
In general, to add interactivity to our HTML/CSS websites we need to
So far, we write HTML/CSS for the structure/styles for our page. We will continue 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: <script>
// template
<script src="filename"></script>
// example
<script src="example.js"></script>
All JavaScript code used in the page should be stored in a separate .js file
JS code can be placed directly in the HTML file's body or head (like CSS), but this is poor code
quality. You should always separate content, presentation, and behavior
Here's a basic index.html linked to a basic script.js - feel free to use these to
practice with!
Accessing Elements from the document object
in JavaScript
Accessing an element by id
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"
Listening to Events with JS/HTML
There are a ton of HTML tags that are used to build UI (user interfaces). We'll start with the
humble button today, but we'll learn a few more like input boxes and dropdowns next week.
The <button>
Unlike Java programs, JS programs have no main; they respond to user actions called events
Event-Driven Programming: writing programs driven by user events
Listening & Responding to Events w/ addEventListener
function handleFunction() {
// event handler code
}
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!
}
There are more events, like:
Where does the "document" come from?
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.206
1. HTTP GET
● GET /index.html HTTP/1.1, Host: www.example.com
● Collection of all this information: Request Headers
1. TCP/IP transfer
● Series of tubes...
1. Check: 200 OK?
● Collection of all this information: Response Headers
Parse Page
1. First line: <!DOCTYPE html>
● Ok: need to build a DOM
1. Line-by-line, goes through the HTML
<html>
<head>
<title>My Fancy Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
<body>
<header>...</header>
<nav>...</nav>
<article>
<section>
<p>Hello world: <a href="...">here</a></p>
<p>Welcome!</p>
<img src="...">
<a href="...">citation</a>
</section>
</article>
<footer>
<a href="..."></a>
</footer>
</body>
</html>
<html>
<head>
<title>My Fancy Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
<body>
<header>...</header>
<nav>...</nav>
<article>
<section>
<p>Hello world: <a href="...">here</a></p>
<p>Welcome!</p>
<img src="...">
<a href="...">citation</a>
</section>
</article>
<footer>
<a href="..."></a>
</footer>
</body>
</html>
<html>
<head>
<title>My Fancy Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
<body>
<header>...</header>
<nav>...</nav>
<article>
<section>
<p>Hello world: <a href="...">here</a></p>
<p>Welcome!</p>
<img src="...">
<a href="...">citation</a>
</section>
</article>
<footer>
<a href="..."></a>
</footer>
</body>
</html>
<html>
<head>
<title>My Fancy Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
<body>
<header>...</header>
<nav>...</nav>
<article>
<section>
<p>Hello world: <a href="...">here</a></p>
<p>Welcome!</p>
<img src="...">
<a href="...">citation</a>
</section>
</article>
<footer>
<a href="..."></a>
</footer>
</body>
</html>
<html>
<head>
<title>My Fancy Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
<body>
<header>...</header>
<nav>...</nav>
<article>
<section>
<p>Hello world: <a href="...">here</a></p>
<p>Welcome!</p>
<img src="...">
<a href="...">citation</a>
</section>
</article>
<footer>
<a href="..."></a>
</footer>
</body>
</html>
<html>
<head>
<title>My Fancy Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
<body>
<header>...</header>
<nav>...</nav>
<article>
<section>
<p>Hello world: <a href="...">here</a></p>
<p>Welcome!</p>
<img src="...">
<a href="...">citation</a>
</section>
</article>
<footer>
<a href="..."></a>
</footer>
</body>
</html>
Another Request:
1. GET: styles.css
2. Schedule: restyle.
And it then combines into a "Render Tree", which is the combination of visible content and the
rules for how it looks.
Finally, we paint
Questions?