I need to make URL from some custom string values dynamically. I have doubt on encodeURIComponent(string)
function whether it has cross browser issues does anyone experienced trouble with this function (I am not using any JS libraries)?
Asked
Active
Viewed 1,675 times
6

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

DSharper
- 3,177
- 9
- 29
- 47
-
Also see: http://stackoverflow.com/questions/75980/when-are-you-supposed-to-use-escape-instead-of-encodeuri-encodeuricomponent – Christophe Roussy Jun 13 '16 at 08:32
2 Answers
9
encodeURIComponent
works as advertised in a cross-browser fashion. Use it in preference to escape
.

Eamon Nerbonne
- 47,023
- 20
- 101
- 166
-
*"Use it in preference to `escape`."* Er, yes, since `escape` doesn't do URI-encoding at all. It does something similar, but different. – T.J. Crowder Dec 10 '10 at 12:31
-
1There's no reason I can think of to ever use `escape` (it's just misdesigned) - but it's similar enough to be confusing, hence the warning. – Eamon Nerbonne Dec 10 '10 at 23:27
6
Go for it. This is exactly what encodeURIComponent
is for and it's well-supported across browsers. I've never heard of a serious issue with it on any major implementation.

T.J. Crowder
- 1,031,962
- 187
- 1,923
- 1,875