Code Bytes · "Web Fundamentals" · Episode 1 · How the web works
What happens when you open a website
When you open a website, your browser asks and a server answers. The browser finds out which server has the site's name, sends a request for the page, and gets back an HTML file with the page's words and layout. It then asks again for every other piece the page needs, such as pictures and styles, and puts all the pieces together on your screen.
What a server is
A server is a computer somewhere that stores a website's files and sends them to anyone who asks. It is not a special kind of magic box: it is a computer (plus the software running on it) whose job is to answer requests, from phones, laptops and tablets all over the world.
Your browser is the other side of the conversation. It is the program you use to visit websites, and its job is to ask.
Step 1: find the right server
You type an address, like example.com. There are a huge number of servers on the internet, so the first thing your browser does is find out which server has that name. Once it knows, it can talk to that server directly.
Step 2: ask for the page
Your browser sends that server a request: "please send me your page."
Step 3: the answer is HTML
The server answers with a file of HTML. HTML holds the page's words and its layout: what the heading says, which paragraphs come next, and where things go.
Step 4: ask again for each piece
Your browser reads the HTML and spots more things the page needs, such as pictures and styles (the files that say what colours, fonts and sizes to use). The HTML only names them, so the browser sends a new request for each one, and the server answers each one.
Step 5: put it all together
When the pieces arrive, the browser puts them all together and draws the finished page on your screen. All of that usually happens in well under a second.
Try it yourself
On a laptop or desktop, open any website in Chrome, Edge or Firefox, then press F12 (or right-click the page and choose Inspect) and click the Network tab. Now reload the page.
Each row that appears is one request your browser sent, with the server's answer. Look at the Type column: you'll see the page itself (often marked "document"), then pictures (png, jpg, svg, webp), styles ("stylesheet" or css) and scripts. A busy news site can send well over a hundred requests just to show one page.
Good to know
- No code is run in this episode. The web course's code (HTML, then CSS, then JavaScript) starts a couple of lessons from now.
- "Finds out which server has that name" is called a DNS lookup: it turns a name like example.com into the server's numeric address (its IP address). Network Bytes goes deeper into how that works.
- The request and the answer are messages in a format called HTTP (or HTTPS, the secure version). With HTTPS, the browser and server also set up a secure connection first. The video skips that step.
- This is simplified. Some pieces can come from other servers, browsers can reuse files saved from an earlier visit (the cache) instead of asking again, several requests can travel over one connection at the same time, and a browser may also ask for extras such as the little site icon in the tab.
- example.com is a domain reserved for examples, so it doesn't belong to any real business.
Answer to the comment question
Your browser asks for the page, then for each of its three pictures. How many requests is that in total?
4 requests. One request for the page (its HTML file), then one request for each of the three pictures: 1 + 3 = 4.
The question sets its own rule: the page, then each of three pictures, and nothing else. On a real website the total is usually higher, because the browser also asks for styles, scripts and other files, and lower if some files are already saved in the cache.
Test your knowledge
Quick quiz: What happens when you open a website
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- MDN Web Docs, "How the web works": browsers and servers, finding the server for a web address, and the request and answer that bring back a page
- MDN Web Docs, "What is a web server?": a web server is a computer that stores a website's files and sends them to browsers that ask
- MDN Web Docs, "An overview of HTTP": the browser first gets the HTML document, then requests the other files the page needs, such as images and style sheets, and puts the page together
- IANA, "Example Domains": example.com is reserved for use in examples
Video transcript
What really happens when you open a website? Your browser asks. A server answers.
A server is a computer somewhere that stores a website and sends it to anyone who asks.
You type an address, like example dot com. First, your browser finds out which server has that name.
Then it sends a request: please send me your page.
The server answers with a file of HTML: the page's words and layout.
Your browser reads it, spots more things it needs, like pictures and styles, and asks for each one too.
Then it puts all the pieces together on your screen.
Your browser asks for the page, then for each of its three pictures. How many requests is that in total? Comment your answer below!
That's your byte.
More from "Web Fundamentals"
- "Web Fundamentals" EP3What HTML is
- "Web Fundamentals" EP4Tags and elements
- "Web Fundamentals" EP5Semantic HTML