Sub topic 2
From keystrokes to a navigation
This chapter covers the step before any network traffic: how the browser decides what you typed and turns it into a navigation. It assumes you can read basic JavaScript and know the URL parts from the previous chapter (scheme, host, port, path, query, fragment). Nothing here needs a network connection, so every example runs locally.
The address bar is two tools in one
Most browsers use one box for two jobs. It can open a web address, and it can send a query to a search engine. The browser has to pick one before it does anything else, and it picks using heuristics, not a formal grammar. That is why example.com opens a site, how to tie a tie opens search results, and an odd input like cats can behave differently across browsers or settings.
The browser does this work in its own process, not in the page. The address bar belongs to the browser's user interface, so no web page runs, and no page script can see what you type until a navigation commits.
What happens while you type
Before you press Enter, the browser is already working on each keystroke:
- It matches the text against history, bookmarks and open tabs to build a list of suggestions.
- It may ask the default search engine for query suggestions. This is why typed text can leave your machine before you press Enter.
- It picks a default action for the top row, either "navigate to this URL" or "search for this text", and labels it in the dropdown.
- For a likely navigation, it may start work early, such as a DNS lookup or a connection setup. This is called a speculative connection. The browser guesses the work is needed and discards it if you change your mind.
The speculative work is why the first request after you press Enter is sometimes faster than the timings in the later chapters suggest. It also means a typed-but-never-submitted address can sometimes cause network activity.
Classifying the input
The core decision is whether the text is a URL, a host name that needs a scheme, or a search query. The common rules are:
- Text with an explicit known scheme, such as
https://example.com, is a URL. - Text with spaces is a search query, because a host name cannot contain spaces.
- Text that looks like a host (a dotted name, an IP address,
localhost, or an IPv6 address in brackets) gets a default scheme and becomes a URL. - Anything else is a search query.
The table shows typical outcomes. Browsers differ in the details, so treat it as a model, not a specification.
| You type | Typical interpretation | Why |
|---|---|---|
| `https://example.com/a?b=1` | Navigate | Explicit scheme |
| `example.com/docs` | Navigate, scheme added | Dotted host name with a path |
| `192.168.1.1` | Navigate, scheme added | Looks like an IPv4 address |
| `localhost:3000` | Navigate, scheme added | Known special host name with a port |
| `how to tie a tie` | Search | Contains spaces |
| `cats` | Usually search | Single word without a dot |
| `example.com:8080/a` | Navigate | Host with port, not a scheme |
The last row is a trap for parsers. The text example.com:8080/a looks like a scheme (example.com) followed by a colon, because scheme names may contain dots. A browser must prefer the host-and-port reading when the part before the colon is not a known scheme.
A small classifier you can run
This function imitates the decision using the standard URL class. It is a teaching model, not what a browser ships. Real browsers also consult history, a list of top-level domains, and settings. Run it with Node.js 18 or later.
const KNOWN_SCHEMES = new Set(['http:', 'https:', 'file:', 'ftp:', 'about:']);
function classify(input) {
const text = input.trim();
if (text === '') return { kind: 'empty' };
// A host name cannot contain whitespace.
if (/\s/.test(text)) return { kind: 'search', query: text };
// 1. Explicit, known scheme: use the text as a URL.
try {
const url = new URL(text);
if (KNOWN_SCHEMES.has(url.protocol)) {
return { kind: 'navigate', url: url.href };
}
} catch {
// Not a valid absolute URL; try the next rule.
}
// 2. Add a default scheme and see whether the result looks like a host.
try {
const url = new URL('https://' + text);
const host = url.hostname;
const looksLikeHost =
host === 'localhost' || host.includes('.') || host.includes(':');
if (looksLikeHost) return { kind: 'navigate', url: url.href };
} catch {
// Invalid host; fall through to search.
}
// 3. Everything else is a search query.
return { kind: 'search', query: text };
}
const samples = [
'example.com',
'example.com:8080/a',
'localhost:3000',
'http://example.com/x?y=1',
'how to tie a tie',
'cats',
'192.168.1.1',
];
for (const s of samples) {
const r = classify(s);
console.log(`${s} -> ${r.kind} ${r.url ?? r.query}`);
}The output is:
example.com -> navigate https://example.com/
example.com:8080/a -> navigate https://example.com:8080/a
localhost:3000 -> navigate https://localhost:3000/
http://example.com/x?y=1 -> navigate http://example.com/x?y=1
how to tie a tie -> search how to tie a tie
cats -> search cats
192.168.1.1 -> navigate https://192.168.1.1/Notice that new URL('localhost:3000') succeeds and reports the protocol localhost:. The scheme check in step 1 rejects it, so the code falls through to step 2. Without the known-scheme list, the function would wrongly treat it as a URL with an unknown scheme.
The sketch uses https:// as the default scheme. Real browsers vary. Many now try HTTPS first and fall back to HTTP if the connection fails, and many keep http:// for localhost. The next chapters explain why the scheme matters so much.
A search is also a navigation
The diagram ends in one place for both branches. A search query is turned into an ordinary URL, for example https://search.example/?q=how+to+tie+a+tie, and from that point the browser treats it like any other address. The remaining chapters apply to search results pages as well.
Starting the navigation
Once the browser has a URL, it starts a navigation. Several things happen before any packet is sent:
- Unload checks. If the current page registered a
beforeunloadhandler, the browser may ask whether you really want to leave. This is the only point where the old page can object. - A navigation request is created. It records the URL, the method (
GETfor a typed address), and a flag showing it was initiated by the user and not by a script. - Local state is consulted. The browser checks its cache, its HSTS list and, if one is registered for the site, a service worker. The next chapter covers these checks.
- The old page stays on screen. The browser keeps showing the current page, often with a loading indicator, until the response arrives and is ready to commit. A slow server therefore looks like "nothing happened".
You can see the result from inside the page after it loads. The Navigation Timing entry records how the load began:
const [nav] = performance.getEntriesByType('navigation');
console.log(nav.type); // 'navigate' for a typed address
console.log(nav.startTime); // 0, the start of the navigation timelineThe type value is navigate for a typed address or link click, reload for a refresh, and back_forward for history navigation.
Edge cases worth knowing
- Fragment-only changes. Typing the same URL with a different
#fragmentscrolls the page and does not fetch anything. This is a same-document navigation. The scheme, host, path and query must all be unchanged. - Internationalized names. A host such as
bücher.exampleis converted to an ASCII form (xn--bcher-kva.example) before DNS sees it. TheURLclass shows this inurl.hostname. - Confusing lookalikes. Because of that conversion, browsers sometimes display the ASCII form instead of the Unicode name when it could mislead you.
- Corporate and local names. A single-word name like
intranetmay be a search or a local host, depending on the browser, its settings and your network's search domains. If something works at the office and searches at home, this is the usual cause. - Private data. Search suggestions may send partial text to your search provider. Most browsers let you turn this off.
Exercise
A user types example.com:8080/status into the address bar. Why can a naive parser misread this as a URL with the scheme example.com, and what does a browser do to avoid the mistake?
Show answer
Scheme names may contain letters, digits, plus signs, hyphens and dots, so example.com: is syntactically a valid scheme followed by an opaque path. A browser avoids this by checking the text before the colon against its list of known schemes (such as http: and https:). When it does not match, the browser treats the text as a host name with a port, adds a default scheme, and navigates to a URL such as https://example.com:8080/status.
Exercise
You change the address from https://example.com/docs#intro to https://example.com/docs#setup and press Enter. No network request appears in the Network panel. Why, and what kind of navigation was this?
Show answer
Only the fragment changed. The fragment is never sent to the server, and the scheme, host, path and query are the same. The browser treats this as a same-document navigation. It scrolls to the element with the matching id and updates history, but it does not fetch or reload the page.