dracoblue.net

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

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files

About