Reputation: 10131
From old post: Should I use encodeURI or encodeURIComponent for encoding URLs?, it said:
encodeURI assumes that the input is a complete URI that might have some
characters which need encoding in it.
encodeURIComponent will encode everything with special meaning, so
you use it for components of URIs such as
What if I need to encode the URI
as query string parameter?
e.g.
var url = "http://example.com/?next=" + encodeURI(url)
or
var url = "http://example.com/?next=" + encodeURIComponent(url)
Upvotes: 7
Views: 25816
Reputation: 921
If U need to encode the URI
as a query string parameter, then you should definitely go with (2)
var url = "http://example.com/?next=" + encodeURIComponent(query_url);
take for example google translator, whenever you type the address in the Translate Section, The address is converted to a URI Component and then passed on to the google servers
If any URL need to be used as a component, then encodeURIComponent(String);
is your best bet
Upvotes: 2
Reputation: 31141
If you want to encode a query string, use encodeURIComponent
. The reason is simple: among a few other chars, it'll encode the forward slash and amersand that encodeURI
will not.
encodeURIComponent
What's the use? Say you want to encode a URL and pass it in the query string, this will let you encode all the characters so you get something like this:
encodeURIComponent('http://abc.com/my page.html?name=bob&foo=bar')
to get the result
"http%3A%2F%2Fabc.com%2Fmy%20page.html%3Fname%3Dbob%26foo%3Dbar"
You can now safely pass that as a query string like so:
http://mysite.com/foo=http%3A%2F%2Fabc.com%2Fmy%20page.html%3Fname%3Dbob%26foo%3Dbar
Notice how both URLs have a query string parameter foo
but that's OK because the encoded URL has that encoded. The entire foo
parameter is
http%3A%2F%2Fabc.com%2Fmy%20page.html%3Fname%3Dbob%26foo%3Dbar
There is no conflict with the foo=bar
in the second, encoded, URL.
encodeURI
Now, if you want to encode a complete URL that you have already, use encodeURI
.
encodeURI('http://abc.com/my page.html?name=bob&foo=bar')
Will give you
"http://abc.com/my%20page.html?name=bob&foo=bar"
Notice how that keeps the URL valid and, in this instance, only encodes the space. If you were to run encodeURIComponent
on that, you'd get the mess you see in my first example.
What characters are encoded?
As yabol commented in your first post, this page shows you the Differences between encodeURI, encodeURIComponent, and escape: lower ASCII characters. You notice specifically that encodeURIComponent
encodes the following chars that encodeURI
does not:
chr encodeURI(chr) encodeURIComponent(chr)
+ + %2B
/ / %2F
@ @ %40
# # %23
$ $ %24
& & %26
, , %2C
: : %3A
; ; %3B
= = %3D
? ? %3F
Your question
You are correct in using encodeURIComponent
because you're encoding a URL for a query string. This goes back to my first example. If your query-string URL (the one you're encoding) has a query string, you want that to be part of next
, not part of your main URL.
Wrong
"http://example.com/?next=" + encodeURI('http://abc.com/my page.html?name=bob&foo=bar')
"http://example.com/?next=http://abc.com/my%20page.html?name=bob&foo=bar"
Your example.com url has two query string parameters: next
and foo
Right
"http://example.com/?next=" + encodeURIComponent('http://abc.com/my page.html?foo=bar')
"http://example.com/?next=http%3A%2F%2Fabc.com%2Fmy%20page.html%3Fname%3Dbob%26foo%3Dbar"
Your example.com url contains only one query string parameter: next
Upvotes: 41
Reputation: 167
The second answer in the question you linked already says it pretty clearly: "If you're encoding a string to put in a URL component (a querystring parameter), you should call encodeURIComponent.
If you're encoding an existing URL, call encodeURI."
So in your example encodeURIComponent is the right one.
Upvotes: 1