6

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)?

DSharper
  • 3,137
  • 9
  • 28
  • 46

2 Answers2

9

encodeURIComponent works as advertised in a cross-browser fashion. Use it in preference to escape.

Eamon Nerbonne
  • 45,251
  • 19
  • 95
  • 164
  • *"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
  • 1
    There'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
  • 959,406
  • 173
  • 1,780
  • 1,769