blog.openboxsoftware.com » read post

javascript escape vs encodeURIComponent

  • April 6th, 2006
  • Posted by Damon Ridgway

I thought I might share my pain with you…

I came across a situation where I had to pass this nasty character “Ö” in a querystring.

This querystring variable was encoded using the javascript escape method. The result was that the encoded character did not appear when trying to request (decode) the querystring variable *poof* (and I’m not sure why Word highlighted that…). What we found (shot Norm) was that because the javascript escape method does not encode non-ASCII characters normally it was simply being eliminated - a problem for our apps, as they mostly (if not all) use UTF8 decoding as per the global settings in the webconfig.

The moral of the story is that we should be using encodeURIComponent when encoding at a querystring variable level or encodeURI when attempting to encode an entire URL string (they both use UTF8 encoding).

encodeURIComponent: encodes all characters
encodeURI: will not encode “:”, “/”, “;”, and “?”

MSDN JScript Reference:

The escape method returns a string value (in Unicode format) that contains the contents of charstring. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as “%20.”

Characters with a value greater than 255 are stored using the %uxxxx format.

Note The escape method should not be used to encode Uniform Resource Identifiers (URI). Use encodeURI and encodeURIComponent methods instead.

Comparing escape(), encodeURI(), and encodeURIComponent()

Core JavaScript 1.5 Guide:Predefined Functions:escape and unescape Functions

4 People had this to say...

Pat Said...

Thank you, thank you, thank you !!!!
I’ve spent a day ferreting amongst Perl, JavaScript, AJAX, mysql and the aspirin bottle, trying to solve exactly the same problem and never finding the right combination. After reading your article and following the first link in it I am able to say “Eureka!”.
In case you haven’t spotted it, I’m VERY grateful :-)

  • June 23rd, 2006 at 2:06 pm
Mark Said...

I haven’t explored this personally; it sounds like a good tip. Curious, though…you’ve cited Microsoft’s JScript documentation on this. It’s not a Microsoft-specific solution, is it?

  • August 7th, 2006 at 2:40 pm
Damon Said...

No, it is not Microsoft specific. It is an ECMA-262 (ECMAScript) standard function. I believe the latest versions of IE, Netscape, Mozilla etc. are all JavaScript ECMA-262 compliant.

  • August 9th, 2006 at 11:21 am
Magirus Said...

Many, many thanks.
This was a real gotcha and I was just about to again blame me for living in a country where they use strange letters and diacritical marks. All, everyone and everything should use at least UTF8…

  • August 24th, 2006 at 6:06 pm

Want your say?

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>