HTML Basics
HTML URL Encoding
URLs can only contain a limited set of characters. URL encoding, also called percent-encoding, converts unsafe characters into a safe format so links and form data work reliably.
What is URL Encoding?
A URL can only use a specific set of ASCII characters. Anything outside that set — spaces, accented letters, and many symbols — must be replaced by a percent sign followed by two hexadecimal digits. This process is called URL encoding or percent-encoding.
For example, a space becomes %20. This is why a search for "web developer" appears in the address bar as web%20developer. Encoding keeps the URL unambiguous so servers read it correctly.
Common Encodings
| Character | Encoded as |
|---|---|
| (space) | %20 (or + in query strings) |
| ! | %21 |
| # | %23 |
| & | %26 |
| + | %2B |
| / | %2F |
| ? | %3F |
| = | %3D |
Example: An Encoded Link
<!DOCTYPE html>
<html>
<body>
<p>Search for a job with spaces safely encoded:</p>
<a href="https://myinternships.in/search?q=web%20developer%20intern">
Search "web developer intern"
</a>
</body>
</html>More Examples
<a href="https://example.com/jobs?role=data%20analyst&city=new%20delhi">
Data analyst jobs in New Delhi
</a>const term = "front end & design";
const url = "https://example.com/search?q=" + encodeURIComponent(term);
console.log(url);
// https://example.com/search?q=front%20end%20%26%20designIn JavaScript, use encodeURIComponent() to safely encode a single value like a search term or parameter. It handles spaces, &, =, and other special characters for you.
Do not leave raw spaces or & in a URL. Unencoded characters can break the link or cause a query parameter to be misread by the server.
Key Takeaways
- URLs allow only a limited set of characters.
- Percent-encoding replaces unsafe characters (space becomes %20).
- Query strings sometimes use + for a space.
- Use encodeURIComponent() in JavaScript to encode values.
