EncodeURIComponent () with browsers and [ä ö å] characters

I have a problem with encodeURIComponent() , because it works differently than browsers (tested with Chrome and Firefox):

  • encodeURIComponent('ä') returns %C3%A4
  • escape('ä') returns %E4
  • Chrome / Firefox encodes ä in x-www-form-urlencoded forms as %E4

So, why does encodeURIComponent behave differently than all the others (mostly browsers)? This actually causes problems, as some websites do not realize that I am trying to feed them. This website is http://verkkopalvelu.vrk.fi/Nimipalvelu/default.asp?L=1 (click "Etunimihaku" because it is based on iframe).

Is encodeURIComponent broken and how should this be fixed? What would be the correct way to encode ä ö å type characters? escape() seems to encode the same as those browsers, but escape() deprecated.

I tested browsers with Fiddler, and also the Console / Network tab shows the encoding as %E4 when I submit the form. Also a test link here: A http://jsfiddle.net/tcyfktvg/1/

+5
javascript
source share
1 answer

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.

+3
source share

All Articles