Difference between encodeURIComponent() and encodeURI() in JavaScript
As you may know, in javascript, these functions allow encoding a URI(Uniform Resource Identifier) by replacing each instance of some characters with one, two, three, or four escape sequences representing the UTF-8 encoding of the character.
Difference between encodeURIComponent() and encodeURI()
There are no big differences, the only difference is that the encodeURI() function encodes special characters, except: , /? : @ & = + $ #
while the encodeURIComponent() function encodes special characters and in addition the above characters(, /? : @ & = + $ #
).
[st_adsense]
Example :
var url = 'https://stackhowto.com/search?q=' var req = 'St@ckHowTo' // encode only the string 'req' with encodeURIComponent var url1 = url + encodeURIComponent(req) console.log(url1) //Output: https://stackhowto.com/search?q=St%40ckHowTo // encode only the string 'req' with encodeURI var url2 = url + encodeURI(req) console.log(url2) //Output: https://stackhowto.com/search?q=St@ckHowTo[st_adsense]