encodeURIComponent() not broken. It encodes characters using the UTF-8 char set. Always. ( ECMAScript 3rd Edition (ECMA-262) p. 82)
escape() uses Unicode for encoding ( ECMAScript 1st Edition (ECMA-262) , p. 60). If the Unicode code is <256, then a simple two-letter representation is used, as you see for "ä". If Unicode code> = 256, then an extended four char representation is used with the leading "u". Example: escape("겧") == "%uACA7" .
The problem occurs when the HTTP server receives the encoded URL. He must decode it. But the URL itself does not indicate which encoding was used to create it.
This URL: http://server/%C3%A4 can be http://server/ä if it was encoded by encodeURIComponent() (using UTF-8), but it can also be http://server/ä encoded escape() (using Unicode):
encodeUriComponent("ä") == "%C3%A4" escape("ä") == "%C3%A4"
It depends on the server configuration that it will use to decode the URL. So here is the solution to your problem: find out which URL the HTTP server is encoding, and choose the appropriate encoding method.
Eduard wirch
source share