Difference decodeURIComponent and decodeURI
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.
How to encode and decode a URL in JavaScript ?
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.
Example:
- Open www.google.com and write a search query βgeeks for geeksβ.
- After search results appear, observe the browserβs URL bar. The browser URL will consist of %20 or + sign in place of space.
- The URL will be displayed like: https://www.google.com/search?q=geeks%20for%20geeks or https://www.google.com/search?q=geeks+for+geeks
Note: The browser converted the spaces into + or %20 signs automatically.
Encoding a URL: Encoding in Javascript can be achieved using:
Table of Content
- 1. JavaScript encodeURI Function
- 2. JavaScript encodeURIComponent() Function
- 3. JavaScript escape() function
- 1. JavaScript decodeURI() Function
- 2. JavaScript decodeURIComponent() Function
- 3. JavaScript unescape() Function
- 4. JavaScript querystring Module