<table>
<thead> <!-- table header -->
<tr> <!-- table row -->
<th>Header for col1</th>
<th>Header for col2</th>
<th>Header for col3</th>
</tr>
</thead>
<tbody> <!-- table body -->
<tr>
<td>1st row data for col1</td>
<td>1st row data for col2</td>
<td>1st row data for col3</td>
</tr>
</tbody>
</table>
In short, it's the interface of another application (eg. Yelp website), to which we can make a request and get some data back.
A header is a field within your HTTP request where you can insert extra info about the request you're sending.
For example, for some APIs, you can send a request with info in your header saying that you want the data to comeb back in JSON format.
Specify an "Accept" property within the headers.
headers:
{
Accept: 'application/json'
}
Note that by default, you don't have to specify any headers.
npm (Node Package Manager)
A huge, public, 3rd party library of packages for JavaScript/Node.js.
We use a package called node-fetch to simplify our API requests.
We bring this package into our code by writing:
const fetch = require('node-fetch');
Now we get to use fetch in our code!
Note that while Node.js doesn't have fetch built it, the browser does. That's why you can just use fetch in the browser's dev tools without installing or requiring in additional package.
When dealing with an asynchronous task (like calling an API over a network), we need to await the process.
If you want to await, you have to mark the function async.
async function getRandomDadJoke() {
// Fetching something from an API takes some time. So we wait for it to finish before moving onto the next line of JavaScript code.
const response = await fetch('https://icanhazdadjoke.com/', {
Accept: 'application/json',
});
// Since the response is in JSON format, we need to parse it into JavaScript by using the .json() method on the respnose.
const data = await response.json();
}
HTML
Almost all HTML contents are "nested".
Most tags need an opening and a closing tag (note that the closing ones contain a
/
).meta
tag here.In this example, the
html
tag has two children nested within:head
body
And the
head
tag has two children of its own:meta
tagtitle
tagWe can also say that the
head
tag asmeta
andtitle
as its "inner HTML".The
title
tag has some "text contents", and they are the "Welcome to Week 3!" text.<
a
> TagYou can make a segment of text into a hyperlink by wrapping the segment into a pair of anchor tags (
<a></a>
).href
attribute within thea
tags.That'll make your text into this:
<
img
> Tagimg
is a self-closing tag, as it's not supposed to contain other elements within.<
img
> has two required attributessrc
src
(source) attribute.alt
<
h2
> TagNote the hierarchy of
<h1>
,<h2>
,<h3>
...etc.<h1>
before you have an<h2>
.<
table
>Reference: https://www.w3schools.com/html/html_tables.asp
Lists
If the list is "ordered" (
<ol>
), the browser adds the numeric markers for you.Unordered list:
<ul>
Within each list, you can add list items,
<li>
.Adding JavaScript to the HTML
Use the
<script>
tag to bring in your JS code.You can put this
<script>
tag as the last child of the<body>
element.API
What is an API?
In short, it's the interface of another application (eg. Yelp website), to which we can make a request and get some data back.
A header is a field within your HTTP request where you can insert extra info about the request you're sending.
For example, for some APIs, you can send a request with info in your header saying that you want the data to comeb back in JSON format.
Note that by default, you don't have to specify any headers.
npm (Node Package Manager)
A huge, public, 3rd party library of packages for JavaScript/Node.js.
We use a package called
node-fetch
to simplify our API requests.We bring this package into our code by writing:
Now we get to use
fetch
in our code!Note that while Node.js doesn't have
fetch
built it, the browser does. That's why you can just usefetch
in the browser's dev tools without installing or requiring in additional package.async-await
Checkout this useful article on using async-await.
When dealing with an asynchronous task (like calling an API over a network), we need to
await
the process.If you want to
await
, you have to mark the functionasync
.DOM (Document Object Model)
A brief intro to DOM: https://www.w3schools.com/js/js_htmldom.asp.
The DOM is the browser's mental image of what should be on displayed on the screen.
We can write JavaScript to manipulate what's on the screen.
Targeting a specific tag w/ JS
Let's say we want to target the
<tbody>
element:querySelector
is a method that exists on thedocument
object.It returns the first element that matches a specified CSS selector(s) in the document.
Since
querySelector
returns an element, we can save that element in a constant:Then we can insert some content into this element using
innnerHTML
: