UrlEncode and Decode in JavaScript

While using the default JavaScript functions escape/unescape to (un)escape strings for url parameters, you'll run into issues, since JavaScript threats + as valid character and so on.

At PHPBuilder-Forums I found an

implementation, which does not work, since it only replaces the first + and leaves all other unchanged. That is very much wrong.

A fixed, and working implementation for urlencode can be found here:

function urlencode(str) {
 str = escape(str);
 str = str.replace(/\\+/g,'%2B');
 str = str.replace(/\\%20/g,'+');
 str = str.replace(/\\*/g,'%2A');
 str = str.replace(/\\//g,'%2F');
 str = str.replace(/\\@/g,'%40');
 return str;
In javascript, open source by
@ 11 Jun 2008, Comments at Reddit & Hackernews