URLs are used for locating resources on the internet and intranet.URL have a specific format which browers understands.Servers use the URL to locate the resource and send it to the browser.URL is a string of characters.The characters allowed in the string are:
- Alphabets
- Numeric characters
- Some special characters
If you have a string which contains characters which are not valid in the URL then that string needs to be encoded.This is absolutely necessary since it will create problems for both browser and the server.There could be few scenarios where you would need to encode the URL:
- When the URL consists of characters which are entered by the user
- When you want to specify characters which not part of the reserved characters and are allowed in the URL.
Encoding means replacing certain characters in the unencoded string with equivalent characters.The encoded characters are preceded by % (percentage sign) followed by hexadecimal digits such as %20.
There are built in functions provided by different languages for encoding the strings.For example in JavaScript there is encodeURI() function.This function converts the passed string to equivalent encoded string.
Following are some of the characters and their equivalent string representations.
Character | Encoded UTF-8 value |
space | %20 |
% | %25 |
& | %26 |
‘ | %27 |
( | %28 |
/ | %2F |
: | %3A |
? | %3F |
@ | %40 |
Leave a Reply