v157 · url host parsing

Where a space is legal

"Spaces are not allowed in URLs" is the folk version and it is wrong. A space is forbidden in the host, percent-encoded in the path, and meaningful in the query and fragment. Move the same space through every component and watch the parser treat it four different ways.

Put the space in…

What new URL() produced
componentvalue

All four at once

The same space, in each position, side by side. This is the whole rule in one table.

One space, four components
positionURLresultwhat happened to the space

code path

// Host: forbidden outright, from Chrome 157.
new URL("https://exa mple.com/");        // TypeError

// Path: legal, and serialised as %20.
new URL("https://example.com/a b").pathname;   // "/a%20b"

// Query: legal, and preserved as a space in the decoded value.
new URL("https://example.com/?q=a b").searchParams.get("q");  // "a b"

// Fragment: legal, serialised as %20.
new URL("https://example.com/#a b").hash;      // "#a%20b"

see also