URL encoding in Javascript

I mentioned this in my Ajax and eCards talk, but if you’ve ever had to encode querystring of anything that isn’t ascii, you’ve run into troubles with encode(). The solution is to use encodeURIComponent().

The quick summary is, don’t use escape() use encodeURI() to encode URLs or encodeURIComponent() when encoding string parts that form a query string. In Ajax, always use the latter and form the string yourself.