JavaScript – encodeURI() vs encodeURIComponent()

The purpose of the tutorial is to show you the differences between encodeURI() and encodeURIComponent() functions.

encodeURIComponent()

This method will encode the given string , and if you pass the encoded string to decodeURIComponent(), it will return the original string.

This method will not encode following characters ~!*()'" and it will encode all the characters along with pure URI characters(/ ? : @ & = + $ , – _ . ! ~ * ‘ ( ) #).

This method is helpful, if query string contains URLs.

encodeURIComponent("https://arjunphp.com/php tutorials#iterator loops");
output: "https%3A%2F%2Farjunphp.com%2Fphp%20tutorials%23iterator%20loops"

encodeURI()

This method is not like encodeURIComponent() , it will encode a URL string but not the parameters or hash.

This method will not encode following characters [email protected]#$&*()=:/,;?+' and it will not encode pure URI characters.

encodeURI("https://arjunphp.com/php tutorials#iterator loops");
output : "https://arjunphp.com/php%20tutorials#iterator%20loops"

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

DMCA.com Protection Status