![]() |
VOOZH | about |
Encoding and decoding URLs in JavaScript is essential for web development, especially when making GET requests with query parameters. This process ensures special characters in URLs are correctly interpreted by the server. For instance, spaces are converted to %20 or + in URLs. This guide covers how to use JavaScript functions like encodeURI(), encodeURIComponent(), escape(), decodeURI(), decodeURIComponent(), and unescape() for effective URL encoding and decoding.
Note: The browser converted the spaces into + or %20 signs automatically.
Encoding a URL: Encoding in Javascript can be achieved using:
Table of Content
The encodeURI() function is used to encode complete URI. This function encodes the special character except for (, / ?: @ & = + $ #) characters.
encodeURI( complete_uri_string );Example: This example shows the use of the above-explained approach.
https://www.google.com/searchq=geeks%20for%20geeks
The encodeURIComponent() function is used to encode some parts or components of URI. This function encodes the special characters. In addition, it encodes the following characters: , / ? : @ & = + $ #
encodeURIComponent( uri_string_component );Example: This example shows the use of the above-explained approach.
geeks%20for%20geeks
JavaScript escape() function takes a string as a single parameter & encodes the string that can be transmitted over the computer network which supports ASCII characters. Encoding is the process of converting plain text into ciphertext.
escape( string )Note: The escape() function only encodes the special characters, this function is deprecated.
Exceptions: @ β + . / * _
Example: This example shows the use of the above-explained approach.
https://www.google.com/searchq=geeks%20for%20geeks <br>https%3A//www.google.com/search%3Fq%3Dgeeks%20for%20geeks
Decoding in Javascript can be achieved using
The decodeURI() function is used to decode URI generated by encodeURI().
decodeURI( complete_encoded_uri_string )Example: This example describes the decodeURI() function of JavaScript.
https://www.google.com/searchq=geeks for geeks
The decodeURIComponent() function is used to decode some parts or components of URI generated by encodeURIComponent().
decodeURIComponent( encoded_uri_string_component )Example: This example describes the decodeURIComponent() of Javascript.
geeks for geeks
This function takes a string as a single parameter and uses it to decode that string encoded by the escape() function. The hexadecimal sequence in the string is replaced by the characters they represent when decoded via unescape() function.
unescape(string)Note: This function only decodes the special characters, this function is deprecated.
Exceptions: @ β + . / * _
Example: This example shows the use of the above-explained approach.
https://www.google.com/searchq=geeks%20for%20geeks https%3A//www.google.com/search%3Fq%3Dgeeks%20for%20geeks https://www.google.com/searchq=geeks for geeks https://www.google.com/searchq=geeks for ...
The querystring module provides utilities for parsing and formatting URL query strings. It can be used to encode and decode URL components.
To encode a URL, we can use the querystring.stringify() function to create a query string from an object and then use encodeURIComponent() to encode the resulting string.
Example: This example shows the use of the above-explained approach.
Output:
https://www.google.com/searchq=geeks%20for%20geeks&page=1&sort=descTo decode a URL, we can use the querystring.parse() function to parse the query string into an object and then access the decoded values.
| decodeURIComponent | decodeURI | |
|---|---|---|
| Definition | The decodeURIComponent() function is used to decode some parts or components of URI generated by encodeURIComponent(). | Decoding in Javascript can be achieved using decodeURI function. |
| Syntax | decodeURIComponent( encoded_uri_string_component ) | decodeURI( complete_encoded_uri_string ) |
| Special Character Encoding | It takes encodeURIComponent(url) string so it can decode these characters. | It takes encodeURI(url) string so it cannot decoded characters (, / ? : @ & = + $ #) |
| Example | decodeURIComponent(β%41β) It returns βAβ decodeURIComponent(β%26β): It returns β&β | decodeURI(β%41β): It returns βAβ decodeURI(β%26β): It returns β%26β |
URL encoding and decoding in JavaScript is crucial for seamless web development. By using functions such as encodeURI(), encodeURIComponent(), escape(), decodeURI(), decodeURIComponent(), and unescape(), developers can ensure their URLs are properly formatted and readable by servers. This skill is essential for handling query parameters in GET requests and ensures a smooth user experience on your website.