Differences in Java’s URLEncoder and JavaScript’s unescape()
This afternoon I ran into a problem between Java's URLEncoder and JavaScript's unescape() funtion. Ideally, I'd be able to create a URL in a Java servlet and ship it to the client and use JavaScript's unescape() function to parse the GET variables for the page. And it almost works. Almost.
The problem is the space character. The URLEncoder encodes the space into a plus sign ('+') and not the hex code (%20). The JavaScript unescape() function doesn't convert the plus sign into a space. So I have to do the following in my code:
function parseGETVars() { var retval = []; var urlChunks = String(document.location).split('?'); if (urlChunks[1]) { var pairs = urlChunks[1].split('&'); for (var i = 0; i < pairs.length; ++i) { if (pairs[i]) { var parts = pairs[i].split('='); retval[parts[0]] = unescape(parts[1]).replace(/\+/,' '); } } } }
With the little change, I can use the Java URLEncoder and still get things working. Not too bad, but why on earth are these guys not doing the same conversions?