Skip to content Skip to sidebar Skip to footer

Encodeuricomponent() Difference With Browsers And [ä Ö Å] Characters

I have a problem with encodeURIComponent() as it seems to behave differently than browsers (tested with Chrome and Firefox): encodeURIComponent('ä') returns %C3%A4 escape('ä')

Solution 1:

encodeURIComponent() is not broken. It encodes the chars using UTF-8 char set. Always. (ECMAScript 3rd Edition (ECMA-262) page 82)

escape() uses Unicode for encoding (ECMAScript 1st Edition (ECMA-262) page 60). If the unicode code is < 256 then the simple two letter representation is used, as you see for "ä". If the unicode code is >= 256, then the extended four char representation with a leading "u" is used. Example: escape("겧") == "%uACA7".

The problem arises when a http server receives a encoded URL. It has to decode it. But the URL itself doesn't tell which encoding was used to create it.

This URL: http://server/%C3%A4 could be a http://server/ä if it was encoded by encodeURIComponent() (using UTF-8), but it could also be a http://server/ä encoded by escape() (using Unicode):

encodeUriComponent("ä") == "%C3%A4"
escape("ä") == "%C3%A4"

It's up to the configuration of the server which encoding it will use to decode the URL. So here's the solution to your problem: know which URL encoding the http server expects and choose the appropriate encoding method.

Post a Comment for "Encodeuricomponent() Difference With Browsers And [ä Ö Å] Characters"