Home > Code > Javascript urlencode() Equivalent

Javascript urlencode() Equivalent

This is a rather strange topic for a Business Intelligence blog, and I’ll probably forever kick myself for making it my first post, but nonetheless, I ran into an issue today trying to get around some ridiculous BO messes. One of which was that my hacks had to all be in Javascript. The other was that I had to go back and forth between POSTing and GETing http variables.

Without getting into any more ugly details, the bottom line is that I always knew there was a nifty little PHP function called urlencode(), but I didn’t know exactly what was available in Javasctipt. Thanks to a quick look on phpbuilder, I realized that there was a built-in function simply called escape() that would work just fine. I needed this because I was building a query string (&abc=1&xyz=2&string=blah) but since I was flipping back and forth between GET and POST methods, I needed to encode it.

The escape() function actually worked pefectly for me, but as a side-note, as cass-hacks explains, there is a distinc difference between urlencode() and escape(), and this would be a way to mimic it more accurately:


function urlencode(str) {
return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}


function urldecode(str) {
return unescape(str.replace('+', ' '));
}

VN:F [1.9.1_1087]

Rating: 2.0/5 (1 vote cast)
VN:F [1.9.1_1087]
Rating: 0 (from 0 votes)
Javascript urlencode() Equivalent, 2.0 out of 5 based on 1 rating
Categories: Code Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.