Turn one link into an address that can be fetched
Start with the document https://learn.example/guide/ch1.html and the link ../images/map.svg?size=2#key. Resolving the link against that base produces https://learn.example/images/map.svg?size=2#key. Concatenating a host name and the original string does not implement the parent-directory step. This lesson builds the boundary that interprets an address and passes its components to navigation. Fetching the resource and painting an image are later operations. Keep the original input alongside the resolved address so that a surprising destination can be traced back to its cause.
Trace three representations: base and reference strings, a resolved absolute URL, and the connection information plus request target derived from it. Discovering the path segment images does not make it a host. In an implementation, keep input, resolved URL, and navigation status in separate fields or types instead of repeatedly overwriting one string. A relative reference can replace only the path or replace the host as well; show that difference as a component-level comparison. At this stage no response body or HTTP status exists. Preserving that initial condition prevents the interface from presenting a parsed address as an already fetched page. It also lets a test distinguish an incorrect resolver from a later failure to obtain a document.
Record the connection destination and request target separately
For this HTTPS example, the scheme, host, and any explicit port identify connection information. The usual HTTP request target contains the path and query. The fragment key identifies a location in the document and is not included in that target. Moving from #intro to #summary may therefore use the existing document. Do not expect every URL component to appear in a server log. The teaching implementation rejects unsupported forms, including embedded user information, rather than treating them as ordinary path characters.
| Component | Example value | Later use |
|---|---|---|
| Host | learn.example | Connection name |
| Path | /images/map.svg | Requested resource |
| Query | ?size=2 | Additional request data |
| Fragment | #key | Document location |
A relative reference needs a base URL
The reference next.html resolves to /guide/next.html from either /guide/ch1.html or /guide/. From /guide without the final slash, however, it resolves to /next.html because guide occupies the final path position. A trailing slash changes interpretation. A reference beginning /assets/ starts at the host root, while //static.example/logo.svg replaces the host and inherits the scheme. Change the base and the reference independently in the exercise. Comparing one changed condition at a time reveals which component is responsible for the destination.
Compare an empty reference, ?lang=en, and #summary against the same base. The query-only reference preserves the base path while replacing its query; the fragment-only reference preserves path and query while changing the fragment. A file-name concatenation rule can accidentally put these delimiters into the path. Do not mechanically replace encoded %2F with a slash before resolution either. Resolution, component extraction, and decoding for display have distinct purposes and an order. The initial supported input can remain short ASCII paths, with full encoding and internationalized-host support added through tests against a reference implementation. Record the supported range rather than accepting a plausible destination whose interpretation has not been defined.
String splitting is not a complete URL parser
Splitting on colons or slashes does not correctly cover IPv6 addresses or encoded delimiters. Start with a declared HTTP/HTTPS subset and report unsupported input. The platform URL API is useful as a reference implementation, but calling it is not the same achievement as implementing a parser. Its hostname property excludes a port, while host may include one. Decide whether the caller needs connection information, a serialized display address, or an origin. Those are related outputs with different purposes.
Compare the original input with normalized outputs
After resolution, place the absolute URL, request target, and origin next to one another. This small program uses the platform URL API to establish reference values; test your own parser separately. Adding #key should leave the request target unchanged, while changing the query should change it. Sharing an origin does not prove that two URLs identify the same resource or authorize the same operation. State the question before choosing a comparison. Keep successful and rejected input cases in the same test collection.
Notice that the reference program does not append the URL hash to the request target. Make request-target construction a separate function so changing display serialization cannot silently change the HTTP input. A result type can hold parsed components on success and an error category or source position on failure. Returning an empty string for both failure and an empty component makes a caller unable to distinguish a root resource from a parse error. Declare whether only http and https are accepted. Accepting a scheme syntactically does not itself authorize the browser to launch an arbitrary operation for that scheme. Those are separate contracts, even in a small teaching browser.
const u = new URL('../images/map.svg?size=2#key', 'https://learn.example/guide/ch1.html');
console.assert(u.href === 'https://learn.example/images/map.svg?size=2#key');
console.assert(u.pathname + u.search === '/images/map.svg?size=2');
console.assert(u.origin === 'https://learn.example');Parsing success is not navigation completion
A parsed URL does not guarantee successful name resolution, connection establishment, or an HTTP response. A response may redirect elsewhere or contain a type that the browser does not display as a document. The initial model uses fixed responses and does not send arbitrary external requests. Keep navigation history separate from the currently displayed document. A parsing failure should not erase an already displayed page. Model a failed new navigation and a surviving old document as distinct state so that the interface can explain both.
Exercise: change only the trailing slash and fragment
Resolve c?x=1#end against https://learn.example/a/b and write both the absolute URL and HTTP request target. The answers are https://learn.example/a/c?x=1#end and /a/c?x=1. Add a trailing slash to the base and the resolved path becomes /a/b/c. Change #end to #top and the request target stays the same. Record both changed and unchanged outputs. Explaining that distinction, rather than memorizing one address, demonstrates the boundary between resolution and fetching.
Add relational tests, not just a list of final addresses. Two URLs differing only by fragment should produce the same request target; changing the query should change that target. Compare https://learn.example:443/a with https://learn.example/a using separate questions about display serialization and origin. If the implementation normalizes a default port, record that behavior explicitly in expected results. Finally, submit a relative reference without a valid base and confirm that failure does not reuse a previous successful destination. Run a good input again after the failure and require the same result as a fresh run. This checks the reset and replay contract as well as URL interpretation.