Skip to content

Instantly share code, notes, and snippets.

@ariunbat
Created April 7, 2013 18:52
Show Gist options
  • Save ariunbat/5331911 to your computer and use it in GitHub Desktop.
Save ariunbat/5331911 to your computer and use it in GitHub Desktop.
Escape function for AJAX
function encodeValue(val){
var encodedVal;
if(!encodeURIComponent){
encodedVal = escape(val);
//Fix the omissions
encodedVal = encodedVal.replace(/@/g, '%40');
encodedVal = encodedVal.replace(/\//g, '%2F');
encodedVal = encodedVal.replace(/\+/g, '%2B');
}
else{
encodedVal = encodeURIComponent(val);
//Fix the omissions
encodedVal = encodedVal.replace(/~/g, '%7E');
encodedVal = encodedVal.replace(/!/g, '%21');
encodedVal = encodedVal.replace(/\(/g, '%28');
encodedVal = encodedVal.replace(/\)/g, '%29');
encodedVal = encodedVal.replace(/'/g, '%27');
}
//Clean up the spaces and return
return encodedVal.replace(/\%20/g, '+');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment