Now we all know what is an URL, an Uniform resource locator which is used for locating a document or any other data on web. It could be a website name like www.saphotos.com or an IP address like 192.168.0.105. Mostly people enter names of the websites as they are easy to remember. Isn’t?
Web browsers request the pages from web servers with the help of URLs.
What is URL Encoding?
URL encoding is the process of replacing unprintable or unrecognizable characters with the characters that are universally accepted by web browsers and servers. It is actually the process of converting string into a valid URL format.
Why would you use URL Encoding?
URLs which contain ASCII characters can only be sent over internet. The URLs may contain characters that are out of ASCII set or they may contain some characters which have special meaning or some reserved characters. So, we need to encode the URLs so that they can be transmitted over the internet. URL encoding is also called Percent-Encoding.
Encoding is done by replacing the desired character with three characters, a percentile sign(%) followed by two Hexa decimal digits that corresponds to the certain character in the encoding table.
URLs cannot contain spaces. Spaces are usually replaced by using either ‘+’ or ‘%20’. According to new percent encoding rules, spaces are replaced with ‘+’ not ‘%20’.
When would you use URL ENCODING?
URL Encoding is normally used in HTML Forms. The special characters which we send from forms like “#”, “.”, “/” etc may carry some special meaning. For example, “#” is used to specify an anchor. So, it needs to be encoded as “%23”
The get and post methods of forms perform URL encoding implicitly. As you know, these methods are used to pass parameters in HTML Forms.
An example program
<html> <body> <form method="GET" action="firstjs.html"> <input type="text" name="name1" size="25" value="URL Encoding"> <input type="submit"> </form> </body> </html>
The above program would give the following output.
As we have used the method ”get” in the form, when you click on submit button, the characters in the input area “URL Encoding “would be sent by encoding. Please look at the address bar in the screenshot below, the string “URL Encoding” is appended as name1=URL+Encoding.
Space is encoded as “+”.
Built in methods
Many languages offer support for URL encoding. They have built in functions to perform encoding and decoding.