HTML URL Encode
A URL (Uniform Resource Locator) is the address of a resource on the web. URLs may only contain a limited set of safe ASCII characters. Anything else — spaces, accented letters, emojis, or characters that have a special meaning like & and ? — must be URL-encoded (also called percent-encoding) before it is sent.
Parts of a URL
https://www.example.com:443/search/results.html?q=html+tables&page=2#top
\___/ \_____________/ \_/\__________________/\___________________/\__/
scheme host port path query fragment| Part | Example | Meaning |
|---|---|---|
| scheme | https |
The protocol used to fetch the resource |
| host | www.example.com |
The server's domain name |
| port | 443 |
Network port (usually left out) |
| path | /search/results.html |
Which file or page on the server |
| query | ?q=html+tables&page=2 |
Extra name=value data, separated by & |
| fragment | #top |
A position inside the page; never sent to the server |
How percent-encoding works
An unsafe character is converted to its UTF-8 bytes, and each byte is written as % followed by two hexadecimal digits.
| Character | Encoded | Why |
|---|---|---|
| space | %20 (or + in form data) |
Spaces are not allowed in URLs |
& |
%26 |
Separates query parameters |
? |
%3F |
Starts the query string |
# |
%23 |
Starts the fragment |
/ |
%2F |
Separates path segments |
= |
%3D |
Separates names from values |
é |
%C3%A9 |
Two UTF-8 bytes |
€ |
%E2%82%AC |
Three UTF-8 bytes |
Letters, digits and - _ . ~ never need encoding.
Try it in the browser
JavaScript has built-in functions for this. encodeURIComponent encodes one piece of a URL (such as a search term); decodeURIComponent turns it back.
<label>Text to encode: <input id="src" value="Bánh mì & cà phê?" size="30"></label>
<p>Encoded: <code id="enc"></code></p>
<p>Decoded again: <code id="dec"></code></p>
<script>
const src = document.getElementById('src');
function update() {
const e = encodeURIComponent(src.value);
document.getElementById('enc').textContent = e;
document.getElementById('dec').textContent = decodeURIComponent(e);
}
src.addEventListener('input', update);
update();
</script>encodeURI vs encodeURIComponent
| Function | Leaves alone | Use for |
|---|---|---|
encodeURI() |
: / ? # & = + and other URL syntax |
A whole URL that is already structured |
encodeURIComponent() |
only letters, digits, - _ . ! ~ * ' ( ) |
A single value placed inside a URL |
If you used encodeURI on a search term containing &, the & would stay as is and the server would think a new parameter starts there.
<pre id="out"></pre>
<script>
const term = 'salt & pepper';
const lines = [
'encodeURI: ?q=' + encodeURI(term),
'encodeURIComponent: ?q=' + encodeURIComponent(term),
];
document.getElementById('out').textContent = lines.join('\n');
</script>The first line breaks the query into q=salt and an empty parameter named pepper; the second keeps the whole value together.
Forms encode for you
When a form is submitted with method="get", the browser builds the query string and encodes every value automatically. Spaces become + in this format (named application/x-www-form-urlencoded). The URLSearchParams object does the same job in JavaScript — the sandbox has no server, so this example shows the URL instead of loading it.
<form id="f">
<input name="city" value="Hồ Chí Minh">
<input name="dish" value="phở & bún">
<button>Build URL</button>
</form>
<p><code id="url"></code></p>
<script>
document.getElementById('f').addEventListener('submit', (e) => {
e.preventDefault();
const params = new URLSearchParams(new FormData(e.target));
document.getElementById('url').textContent = 'https://example.com/search?' + params;
});
</script>Tip: Never build URLs by gluing raw user input into a string. Always encode values first, or use
URLSearchParams/ theURLobject, which encode correctly for you.
Note: Browsers often display decoded, friendly URLs in the address bar (for example with Vietnamese letters), but what actually travels over the network is the percent-encoded version.
Test yourself
How is a space written in percent-encoding?
%20 %SP\s
Which function should you use to encode a single search term placed in a query string?
encodeURI()encodeURIComponent()escapeHTML()btoa()
Which part of https://a.com/p?x=1#info is never sent to the server?
/p?x=1#infoa.com