Problem
Which of these two ways for encoding URLs should you use?
Asked by Aditya Shukla
Solution #1
It all depends on what you want to accomplish.
encodeURI assumes that the input is a full URI that may contain some characters that need to be encoded.
encodeURIComponent will encode anything with a special meaning, thus you should use it for URI components like http://www.example.com.
var world = "A string with symbols & characters that have special meaning?";
var uri = 'http://example.com/foo?hello=' + encodeURIComponent(world);
Answered by Quentin
Solution #2
You should use encodeURIComponent to encode a string for use in a URL component (a querystring parameter).
Call encodeURI if you’re encoding an existing URL.
Answered by SLaks
Solution #3
A fantastic debate with examples may be found on xkr.us. To paraphrase their summary:
Answered by BrianFreud
Solution #4
Here’s a quick rundown.
Answered by Frank Wang
Solution #5
For distinct applications, encodeURI and encodeURIComponent are utilized. Some of the differences are as follows:
-_.!*’ is not encoded by encodeURIComponent (). You must replace these characters with a corresponding UTF-8 sequence of characters if you want them to be encoded.
Please see the reference page for additional information about encodeURI and encodeURIComponent. Link to a Source
Answered by Pulkit Aggarwal
Post is based on https://stackoverflow.com/questions/4540753/should-i-use-encodeuri-or-encodeuricomponent-for-encoding-urls