Intro to JavaScript
JavaScript: the DOM // intro to programming in JavaScript
1
What is JavaScript?
2
Nice overview: https://javascript.info/intro
“Client-Side” JavaScript
What is JavaScript’s job within the browser?
3
Nice overview: https://javascript.info/intro
The Possibilities and Limitations of “Client-Side” JavaScript
4
Nice overview: https://javascript.info/intro
5
Browser’s Local Storage / Cookies
Information Services Website
- Google-assigned user id
- Other metadata
Online shopping Website
Amazon
- Amazon-assigned user id
- Other metadata
X
Tracking Company
Third Party Cookies
- Third Party user id
- Third Party user id
“Server-Side” JavaScript (Not in this Class, but FYI)
What is JavaScript’s job on a server (e.g. Node.js)?
6
Client-Side JavaScript Schedule
Week 5 (this week):
�Week 6
�Week 7
7
JavaScript Readings / Videos
LinkedIn Learning — see website for playlists
8
Document Object Model (DOM)
[Reminder from Lecture 4]
A way of representing a document, like a web page, in a way that can be understood by a human and by a computer. Javascript can directly manipulate the DOM dynamically.
9
Selectors
Recall from CSS: selectors are ways of targeting elements in a web page so that we can apply styles to them.
Remember these...
10
...
1 <body>
2 <div class=”title-bar”>
3 <h1>Welcome, Malik</h1>
4 <img id=”profile” src=”images/pic.png” />
5 <hr>
6 </div>
7 <div>Right
8 <ul>
9 <li>list item 1</li>
10 <li>list item 2</li>
11 <li>list item 3</li>
12 </ul>
13 </div>
14 </body>
...
body {
color: grey;
}
h1, li {
text-transform: uppercase;
display: inline-block;
color: #999999;
}
.title-bar {
padding: 5px;
background-color: #EEEEEE;
}
#profile {
width: 100px;
float: left;
margin-right: 20px;
}
11
...
1 <body>
2 <div class=”title-bar”>
3 <h1>Welcome, Malik</h1>
4 <img id=”profile” src=”images/pic.png” />
5 <hr>
6 </div>
7 <div>Right
8 <ul>
9 <li>list item 1</li>
10 <li>list item 2</li>
11 <li>list item 3</li>
12 </ul>
13 </div>
14 </body>
...
body {
color: grey;
}
h1, li {
text-transform: uppercase;
display: inline-block;
color: #999999;
}
.title-bar {
padding: 5px;
background-color: #EEEEEE;
}
#profile {
width: 100px;
float: left;
margin-right: 20px;
}
12
...
1 <body>
2 <div class=”title-bar”>
3 <h1>Welcome, Malik</h1>
4 <img id=”profile” src=”images/pic.png” />
5 <hr>
6 </div>
7 <div>Right
8 <ul>
9 <li>list item 1</li>
10 <li>list item 2</li>
11 <li>list item 3</li>
12 </ul>
13 </div>
14 </body>
...
body {
color: grey;
}
h1, li {
text-transform: uppercase;
display: inline-block;
color: #999999;
}
.title-bar {
padding: 5px;
background-color: #EEEEEE;
}
#profile {
width: 100px;
float: left;
margin-right: 20px;
}
13
...
1 <body>
2 <div class=”title-bar”>
3 <h1>Welcome, Malik</h1>
4 <img id=”profile” src=”images/pic.png” />
5 <hr>
6 </div>
7 <div>Right
8 <ul>
9 <li>list item 1</li>
10 <li>list item 2</li>
11 <li>list item 3</li>
12 </ul>
13 </div>
14 </body>
...
body {
color: grey;
}
h1, li {
text-transform: uppercase;
display: inline-block;
color: #999999;
}
.title-bar {
padding: 5px;
background-color: #EEEEEE;
}
#profile {
width: 100px;
float: left;
margin-right: 20px;
}
14
JavaScript also supports element targeting...
15
Method | Example | Returns |
getElementById() | document.getElementById("my_element") | single element |
querySelector() | document.querySelector("#my_element") document.querySelector("p") document.querySelector(“.my-announcements") | single element |
querySelectorAll() | document.querySelectorAll("p") | list of elements |
getElementsByTagName() | document.getElementsByTagName("div") | list of elements |
getElementsByClassName() | document.getElementsByClassName(".panel") | list of elements |
Recall from CSS: selectors are ways of targeting elements in a web page so that we can apply styles to them.
And once you target an element, �you can change it...
16
DOM Manipulation: Some (of the many) attributes you can set
These are but a few. You can set any element attribute using JavaScript
17
Attribute | Example | Elements |
className | document.querySelector("div").className = “panel"; | all |
innerHTML | document.querySelector("div").innerHTML = “hi!"; | all |
src (for images) | document.querySelector("img").src = “some_image_url” | images only |
href (for links) | document.querySelector("a").href = “http://site.com”; | links only |
... | ... | ... |
DOM Manipulation: Style properties that you can set
These are but a few. You can set any style property using JavaScript
18
Property | Example |
width | document.querySelector("div").style.width = "200px"; |
height | document.querySelector("div").style.width = "200px"; |
background color | document.querySelector("div").style.backgroundColor = "hotpink"; |
border width | document.querySelector("div").style.borderWidth = "5px"; |
padding | document.querySelector("div").style.padding = "10px"; |
display | document.querySelector("div").style.display = "none"; |
... | ... |
A quick look-ahead...
19
Quick Preview of Functions and Events
We will be going over both functions and events in JavaScript next week, however we’re going to start using them today. You don’t need to know how to write them, but you do need to know how to read them...�
20
Function Example
A function is an encapsulated grouping of programming statements that you can invoke on demand...
�const sayHello = () => {� document.querySelector('h1').style.color = 'white';� document.querySelector('h1').innerText = 'Hello!';�};
const sayGoodbye = () => {� document.querySelector('h1').innerText = 'Bye!';�};
sayHello();�sayGoodbye();�sayHello();�sayGoodbye();�
21
Event Handler Example
An event handler is a way that allows you to attach a function to an event. There are many different events that a browser allows you to hook into: click, mouseover, mouseout, drag, scroll, etc.��
EXAMPLE:
<button onclick=”sayHello()”>click me</button>�
22
Summary of the Process of DOM Manipulation
23
Download Today’s Code
24
Activity 1: Manipulating Style Properties
Open 01-style-property-demo
25
Activity 2: Manipulating HTML Element Attributes
Open 02-attribute-demo
26
Activity 3: Together
Open 03-all-of-the-above
27