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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DSharper
  • 3,177
  • 9
  • 29
  • 47

2 Answers2

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
  • 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
  • 1,031,962
  • 187
  • 1,923
  • 1,875