Encode and Decode URI Component – Handling special characters in URL scheme

The encodeURIComponent() and decodeURIComponent() functions are part of the JavaScript standard library, so you can use them in any JavaScript code, including AngularJS applications. You don’t need to include any additional libraries to use these functions.

Here’s how you can use them:

encodeURIComponent()

This function is used to encode a URI component by escaping special characters to make it safe for use in a URL. It takes a string as an argument and returns the encoded string.

var originalString = "my parameter with spaces";
var encodedString = encodeURIComponent(originalString);
console.log(encodedString); // "my%20parameter%20with%20spaces"

decodeURIComponent()

This function is used to decode a URI component that has been previously encoded using encodeURIComponent(). It takes an encoded string as an argument and returns the decoded string.

var encodedString = "my%20parameter%20with%20spaces";
var decodedString = decodeURIComponent(encodedString);
console.log(decodedString); // "my parameter with spaces"

You can use these functions in your JS code whenever you need to encode or decode URL components to ensure proper URL handling, especially when dealing with special characters or spaces in URLs.

You might also like