Sub topic 2
Anatomy of a URL
This chapter assumes you know what a browser and a web page are, and that you can run JavaScript in Node.js or a browser console. A URL (Uniform Resource Locator) tells a client where a resource lives and how to ask for it. The browser reads it first, so every later step depends on it: DNS lookup, connecting, and the HTTP request.
The six parts
Take this URL:
https://example.com:8443/docs/intro?lang=en&page=2#setup| Part | Value | Role |
|---|---|---|
| Scheme | `https` | The protocol to use. |
| Host | `example.com` | The server's name, resolved by DNS. |
| Port | `8443` | The TCP port on that server. |
| Path | `/docs/intro` | Which resource on the server. |
| Query | `lang=en&page=2` | Extra parameters, as `key=value` pairs joined by `&`. |
| Fragment | `setup` | A position inside the resource. |
Where each part goes
The scheme selects the protocol. The host is resolved to an IP address, which the next chapter covers. The port is optional. When omitted, the scheme's default applies: 80 for http and 443 for https.
The path and query travel in the HTTP request line, for example GET /docs/intro?lang=en&page=2 HTTP/1.1. The host goes in the Host header.
The fragment is never sent. The browser uses it locally, for example to scroll to an element whose id is setup.
Parsing a URL in code
Hand-splitting URLs with string operations tends to break on edge cases. Use the built-in URL class instead. It works in browsers and Node.js.
const url = new URL("https://example.com:8443/docs/intro?lang=en&page=2#setup");
console.log(url.protocol); // "https:"
console.log(url.hostname); // "example.com"
console.log(url.port); // "8443"
console.log(url.pathname); // "/docs/intro"
console.log(url.search); // "?lang=en&page=2"
console.log(url.hash); // "#setup"
console.log(url.searchParams.get("lang")); // "en"
console.log(url.origin); // "https://example.com:8443"The origin is the scheme, host, and port together. The same-origin policy, covered in a later chapter, is built on it.
Percent-encoding
Only a limited set of characters may appear in a URL. Others, such as spaces and non-ASCII letters, are written as % followed by the byte in hexadecimal. A space becomes %20. Reserved characters like & and # have meaning, so a value containing them must be encoded.
const url = new URL("https://example.com/search");
url.searchParams.set("q", "fish & chips #1");
console.log(url.href);
// https://example.com/search?q=fish+%26+chips+%231Here searchParams encoded the space as + and the & and # as %26 and %23. Building query strings by plain concatenation skips this step. A raw & in a value then ends the parameter early.
Common errors
- Assuming the server can see the fragment. It cannot.
- Treating
http://example.comandhttps://example.comas the same origin. They differ in scheme and port. - Forgetting that paths are case-sensitive on most servers, while hostnames are not.
Exercise
A browser requests https://shop.example.com/cart?item=7#top. Which parts of the URL appear in the HTTP request line or Host header, and which part stays in the browser? What port does the browser connect to?
Show answer
The path /cart and query item=7 appear in the request line, and shop.example.com appears in the Host header. The fragment #top stays in the browser. No port is written, so the browser uses the https default, 443.