Url Encode in HTML
? URL Encode in HTML
URL encoding (also known as percent-encoding) ensures that characters in a URL are transmitted correctly over the internet by replacing unsafe characters with a % followed by two hexadecimal digits.
? Why URL Encoding is Needed
URLs can only contain certain characters:
Letters (
A–Z,a–z)Digits (
0–9)A few special characters:
- _ . ~
Other characters like spaces, ampersands, slashes, quotes, etc. must be encoded.
? Common URL Encoded Characters
| Character | Encoded Value |
|---|---|
| Space | %20 or + |
" | %22 |
# | %23 |
% | %25 |
& | %26 |
' | %27 |
/ | %2F |
: | %3A |
= | %3D |
? | %3F |
? Example
Original URL:
https://example.com/search?query=HTML & CSSEncoded URL:
https://example.com/search?query=HTML%20%26%20CSS? Encoding in Forms
Browsers automatically URL-encode form data when submitted:
<form action="/search" method="get"> <input type="text" name="q" value="HTML & CSS"> <button type="submit">Search</button></form>When submitted, this results in:
/search?q=HTML%20%26%20CSS??? Encode Using JavaScript
const text = "HTML & CSS";const encoded = encodeURIComponent(text);console.log(encoded); // "HTML%20%26%20CSS"? When to Use
Form data in query strings
API request parameters
Embedding URLs in HTML attributes like
<a href="">
Would you like a downloadable URL encoder tool or a live example to test URL encoding right in the browser?