Skip to content

Instantly share code, notes, and snippets.

@davidvanvickle
Created July 25, 2012 00:50
Show Gist options
  • Save davidvanvickle/3173685 to your computer and use it in GitHub Desktop.
Save davidvanvickle/3173685 to your computer and use it in GitHub Desktop.
JS urlencode
function URLEncode (clearString) {
var output = '';
var x = 0;
clearString = clearString.toString();
var regex = /(^[a-zA-Z0-9_.]*)/;
while (x < clearString.length) {
var match = regex.exec(clearString.substr(x));
if (match != null && match.length > 1 && match[1] != '') {
output += match[1];
x += match[1].length;
} else {
if (clearString[x] == ' ')
output += '+';
else {
var charCode = clearString.charCodeAt(x);
var hexVal = charCode.toString(16);
output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
}
x++;
}
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment