URL Encoder / Decoder
Encode text to URL format
Using encodeURIComponent/decodeURIComponent - encodes all special characters
Common Encodings:
What is URL Encoding?
URL encoding, also known as percent-encoding, is a method to encode special characters in a URL by replacing them with a percent sign (%) followed by two hexadecimal digits. This ensures that URLs are transmitted correctly over the internet, as URLs can only contain a limited set of characters from the ASCII character set.
When Do You Need URL Encoding?
- Query Parameters: When passing data in URL query strings (e.g., search terms, form data), special characters like spaces, ampersands, and equals signs must be encoded.
- API Requests: When making HTTP requests to APIs, parameters containing special characters need proper encoding to be interpreted correctly.
- File Names: URLs containing file names with spaces or special characters require encoding.
- Internationalization: Non-ASCII characters (like accented letters or Chinese characters) must be encoded for use in URLs.
encodeURI vs encodeURIComponent
JavaScript provides two functions for URL encoding, each with different use cases:
- encodeURIComponent: Encodes all special characters except alphanumeric characters and
- _ . ! ~ * ' ( ). Use this for encoding individual URL components like query parameter values. - encodeURI: Preserves URL structure characters like
: / ? & = #. Use this when encoding a complete URL while preserving its structure.
Common Characters and Their Encodings
Some frequently encoded characters include: space (%20), ampersand (%26), equals (%3D), question mark (%3F), forward slash (%2F), hash (%23), at sign (%40), and plus (%2B).
Tip: When building URLs programmatically, always encode user-provided input to prevent URL injection vulnerabilities and ensure proper URL formatting.