The escape() function, was deprecated and replaced by encodeURIComponent but encodeURIComponent doesn't encode single quote/apostrophe character. Which I need to escape the apostrophes in a persons surname (E.g. 'O'Neill') in an AJAX form. Why would they remove the ability of something they were trying to improve?
EDIT:
So here is a code example to explain the problem more thoroughly. So as you can see the surname 'O'Neill' contains an apostrophe that needs to be escaped when passing the variable in the url. But this would also happen in other places in the form, for instance if an address entered was 'Billy's Tavern'.
<input id='surname' value="O'Neill">
<script>
var get_url = '?surname='+encodeURIComponent($('#surname').val());
$.ajax({
url: get_url
});
</script>
My current solution, using a custom function. My question was just to ask why there is a need for a custom function.
<script>
function customEncodeURIComponent(URI) {
return encodeURIComponent(URI).replace(/'/g, "%27");
}
</script>
<input id='surname' value="O'Neill">
<script>
var get_url = '?surname='+customEncodeURIComponent($('#surname').val());
$.ajax({
url: get_url
});
</script>
See Question&Answers more detail:os