Tools
URI Encoder/Decoder
This online tool encodes or decodes a string using URI1 encoding. URI encoding is used when placing text in a query string, to avoid it being confused with the URI itself. It's commonly used in dynamic server pages, to pass content and instructions to other pages.
URI encoding replaces "unsafe" characters with '%
' followed by their hex equivalent. 7-bit ASCII alphanumerics and the characters "-._~
" are safe and does not need encoding, as defined in RFC3986. Unicode characters and 8-bit ASCII characters are normally first encoded using UTF-8, as defined in RFC2279.
JavaScript functions
The functions used are encodeURIComponent()
and decodeURIComponent()
, but there are also alternative JavaScript functions often (mistankingly) used for the same type of encoding:
escape()
,unesacpe()
- Converts special (non-Latin) characters from ASCII to Hex and back.
Does not encode@*/+
encodeURI()
,decodeURI()
- Encodes special characters, though not the separators which have meaning in a normal URI.
Does not encode~!@#$&*()=:/,;?+'
encodeURIComponent()
,decodeURIComponent()
- Encodes special characters as well as characters that have special meaning in a URI.
Does not encode~!*()'
In conclusion, escape()
should be avoided altogether, since it does not encode the + character, nor can it handle non-ASCII characters correctly. encodeURI()
is only for encoding an entire URI at once, e.g. for passing safely between systems, and will not work for query string parts since the URI separators are not encoded.
Our winner therefore is encodeURIComponent()
, since most of the URI encoding people need are with the purpose of encoding elements to a query string, where they should not be mistanken for part of the URI.
- The term URI should generally be used in place of URL, which refers to a subset the common URI protocols and has been deprecated.