Java Script – encodeURI() & decodeURI()

encodeURI():
This function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character. It returns a new string representing the provided string encoded as a URI.

This method does not encode characters like: , / ? : @ & = + $ * #
Use the encodeURIComponent() method instead.

decodeURI():
This function decodes a Uniform Resource Identifier (URI)  previously created by encodeURI or a similar routine.

let uri = "my test.asp?name=ståle&car=saab";

let encoded = encodeURI(uri);
console.log('encoded',encoded)     // my%20test.asp?name=st%C3%A5le&car=saab

let decoded = decodeURI(encoded);
console.log('decoded',decoded)     // my test.asp?name=ståle&car=saab

Leave a comment

Your email address will not be published. Required fields are marked *