What is the JavaScript equivalent of C# Server.URL

2020-07-05 05:03发布

问题:

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

回答1:

encodeURI()

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



回答2:

No, encodeURIComponent() exactly.



回答3:

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