11

What is the JavaScript equivalent of C# Server.URLEncode?

3 Answers 3

13
encodeURI()

http://xkr.us/articles/javascript/encode-compare/#ref-js-msdn

Sign up to request clarification or add additional context in comments.

Comments

8

No, encodeURIComponent() exactly.

2 Comments

I really find it hard to believe that this is your first answer after 476 questions. Surely it befits something more than this lousy comment of mine :D
This question may become famous, simply because of this answer! However, isn't this answer more correct?
4

There’s a wonderful article on xkr.us comparing javascript’s various escape functions. Do read it for details, but here’s a quick summery:

  • escape() — don’t use: does not understand non-ASCII characters, and does not escape some important URI characters, such as +.
  • encodeURI() — encodes an entire URI: as such, it leaves ? and & unencoded.
  • encodeURIComponent() — encodes a component in a query string (this is usually the one you want, and appears to be the equivalent of Server.URLEncode).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.