Definition:
URL Encoding (also called percent encoding) is the process of converting characters into a format that can be safely transmitted over the internet in a URL.
Some characters in URLs are reserved for special meanings (e.g., ?, &, /), and some are not allowed (like spaces), so they must be encoded.


1. Why URL Encoding is Needed

  • URLs can only contain letters (A–Z, a–z), digits (0–9), and a few special characters (- _ . ~).
  • Any other characters (spaces, punctuation, symbols, Unicode) must be encoded as % followed by two hexadecimal digits.

Example:

Space → %20
@ → %40
₹ → %E2%82%B9

2. How URL Encoding Works

  • Reserved/unsafe characters are replaced with a percent sign (%) + two hexadecimal values representing the character’s ASCII/UTF-8 code.
  • Example: "Hello World!" becomes:
Hello%20World%21
  • Encoding ensures safe transmission in query strings, form submissions, and hyperlinks.

3. Common URL Encodings List

CharacterEncoded ValueDescription
Space%20 or +Whitespace
!%21Exclamation
#%23Hash (anchor)
$%24Dollar sign
%%25Percent
&%26Ampersand
(%28Left parenthesis
)%29Right parenthesis
*%2AAsterisk
+%2BPlus sign
,%2CComma
/%2FForward slash
:%3AColon
;%3BSemicolon
=%3DEquals sign
?%3FQuestion mark
@%40At symbol
[%5BLeft square bracket
]%5DRight square bracket

4. Example in HTML

<!DOCTYPE html>
<html>
<body>

<h2>URL Encoding Example</h2>

<!-- Link with space (encoded) -->
<a href="https://example.com/search?q=Hello%20World">
  Search for "Hello World"
</a>

<!-- Form that submits encoded values -->
<form action="https://example.com/search" method="get">
  <input type="text" name="q" value="HTML & CSS">
  <input type="submit" value="Search">
</form>

<!-- Encoded URL in JavaScript -->
<script>
  let text = "HTML & CSS";
  let encoded = encodeURIComponent(text);
  console.log(encoded); // HTML%20%26%20CSS
</script>

</body>
</html>

5. Functions for URL Encoding

  • JavaScript:
    • encodeURI(url) → Encodes a full URL but keeps : / ? # & safe.
    • encodeURIComponent(str) → Encodes every unsafe character.
  • Server-side languages (PHP, Python, Java, etc.) have similar functions (urlencode, urllib.parse.quote, etc.).

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *