A web address can only contain a limited set of characters. Everything else must be percent encoded: replaced by a % followed by two hex digits.
| Character | Encoded |
|---|---|
| space | %20 |
& | %26 |
? | %3F |
/ | %2F |
é | %C3%A9 |
<a href="search.html?q=cats%20and%20dogs">Search for cats and dogs</a>
Where it matters
- Links with query strings: the part after
?often contains user text. - File names: a file called
my photo.jpgshould be linked asmy%20photo.jpg. It is easier to avoid spaces in file names altogether. - mailto links with a subject, as in Email links.
Forms do it for you
When a form with method="get" is sent, the browser encodes the values automatically.
In JavaScript
encodeURIComponent("cats & dogs"); // "cats%20%26%20dogs"
Use encodeURIComponent when you build a link from user text.
Unencoded ampersand in a value
& inside a value, such as ?q=salt&pepper, starts a new parameter. Encode it as %26.