URL Encoding a string with javascript
Feb.26, 2008 in
Development
The following javascript function returns a URL encoded string so that it is safe to use in URLs or as a GET request:
function URLDecode (encodedString) {
var output = encodedString;
var binVal, thisString;
var myregexp = /(%[^%]{2})/;
while ((match = myregexp.exec(output)) != null
&& match.length > 1
&& match[1] != '') {
binVal = parseInt(match[1].substr(1),16);
thisString = String.fromCharCode(binVal);
output = output.replace(match[1], thisString);
}
return output;
}
You can read more about this function from its source.
Tags: javascript

Leave a Reply