Lesson 1 · Websites from scratch
How the web works
Follow a page from the address bar to your screen.
About 15 minutes
Tab indents. Press Escape, then Tab, to leave the editor.
You type an address, hit Enter, and a page shows up in under a second. In that second your browser reads the address, finds a computer that might be on the other side of the planet, has an encrypted conversation with it, downloads a pile of files and turns them into pixels. Once you know the steps, you'll know why pages are slow, why some break, and how apps like this one keep working with no internet at all.
The address is a set of directions
A web address is a URL (Uniform Resource Locator). It isn't one blob of text. It has parts, and each part tells the browser something different:
https://games.example.com:443/top-scores?game=snake#week
└─┬──┘ └──────┬────────┘ └┬┘ └────┬───┘ └───┬───┘ └─┬─┘
scheme host port path query fragment
- Scheme: how to talk to the server.
httpsmeans HTTP over an encrypted connection. - Host: which computer to ask. This is a domain name, made for humans.
- Port: which "door" on that computer. You almost never type it:
httpsuses 443 by default andhttpuses 80. - Path: which thing on the server you want.
- Query: extra options after the
?, askey=valuepairs joined by&. - Fragment: the part after
#. It never goes to the server. The browser uses it to jump to a spot on the page.
JavaScript has a built-in URL tool that splits an address into these parts for you. Try it.
Challenge
Take a URL apart
Press Run to see the parts of the address. Then change address to a URL of your own that has a path (something after the domain, like /guides/water) and a query (something after a ?, like ?lang=en). Run it again and check the parts make sense.
- Nothing logged yet.
- Your URL has a path after the domain
- Your URL has a query string after a ?
Finding the server: DNS
Computers on the internet find each other by IP address, a number like 203.0.113.42 (IPv4) or a longer one like 2001:db8:85a3::8a2e:370:7334 (IPv6). Nobody wants to memorize those, so the internet has a giant, spread-out phone book called DNS, the Domain Name System.
When you visit games.example.com, the browser first checks whether it looked that name up recently. If not, it asks a DNS resolver (usually run by your internet provider, or one you picked). The resolver works through the DNS hierarchy until it gets an answer, then hands back the IP address. Answers are cached for a while, so the next visit skips this step.
Connecting and asking: TCP, TLS and HTTP
With an IP address in hand, the browser opens a connection to the server. For most sites that's a TCP connection, which makes sure data arrives complete and in order. Then, because the scheme is https, the two sides do a TLS handshake: the server proves who it is with a certificate, and they agree on keys so everything after that is encrypted. (Newer HTTP/3 connections use QUIC, which runs over UDP and does the connection and encryption setup together, but the idea is the same.)
Now the browser can finally ask for the page. It sends an HTTP request:
GET /top-scores?game=snake HTTP/1.1
Host: games.example.com
Accept: text/html
The server sends back an HTTP response: a status code, some headers (information about the response), and the body (the actual file):
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600
<!doctype html>
<html>…
Status codes come in families. 2xx means it worked (200 OK). 3xx means "look somewhere else" (301 Moved Permanently, or 304 Not Modified, which means your cached copy is still good). 4xx means the request was the problem (404 Not Found). 5xx means the server broke (500 Internal Server Error).
From HTML to pixels
The first file back is usually HTML. The browser reads it top to bottom and builds the DOM, a tree of every element on the page. Along the way it finds links to more files: CSS stylesheets, JavaScript, images, fonts. Each one is another HTTP request, often over the same connection.
Then the browser works out the style of every element, does layout (where each box goes and how big it is) and paints the result onto the screen. When JavaScript changes the page later, parts of this repeat.
Step through the whole trip below. Watch where the cache can short-circuit the trip: that's the key idea behind offline apps.
Lab
Loading a web page, step by step
Step through the journey. The diagram lights up the parts involved in each step.
Parse the URL
https://example.com/news?page=2 splits into a scheme (https), a host (example.com), a path (/news) and a query (page=2). Text that isn’t a URL becomes a search instead.
Why this matters offline
Every step above needs the network except one: the cache. If the browser (or a service worker, which you'll meet later in this path) already has a saved copy of a file, it can skip DNS, TCP, TLS and HTTP entirely and use the copy. That's how a well-built web app opens on a plane or in a blackout. No connection? No problem, as long as the files were saved earlier.
Recap
- A URL has parts: scheme, host, port, path, query and fragment. The fragment never leaves your browser.
- DNS turns a domain name into an IP address so the browser knows which server to contact.
- The browser connects (TCP, then TLS for
https), sends an HTTP request, and gets a response with a status code, headers and a body. - The browser parses HTML into the DOM, fetches CSS, JavaScript and images, then does layout and paint.
- A cache can skip the network entirely, which is the foundation of apps that work offline.
Finished this lesson?
Mark it complete to track your progress on this device.