Reputation: 12027
I need a way to encode both '
and &
in a url. Check the following examples:
// "get_records.php?artist=Mumford%20%26%20Sons"
"get_records.php?artist=" + encodeURIComponent("Mumford & Sons");
// "get_records.php?artist=Gigi%20D'Agostinos"
"get_records.php?artist=" + encodeURIComponent("Gigi D'Agostino");
encodeURIComponent
doesn't encode '
. I can use escape
instead, but it's deprecated, I guess. What do I do in this case? Create a custom encoder?
I'll be escaping other characters too: :
, /
, .
, ,
, !
, for example, for the following strings
"11:59"
"200 km/h in the Wrong Lane"
"P.O.D."
"Everybody Else Is Doing It, So Why Can't We"
"Up!"
So creating a custom encoder seems like the best option. Is there an alternative approach that I can use?
Upvotes: 1
Views: 60
Reputation: 1716
You can replace characters before create URL.
' = %27
Check this: http://www.w3schools.com/tags/ref_urlencode.asp
Upvotes: 0
Reputation: 42510
You will have to implement this functionality yourself.
MDN covers this exact topic. Extending their proposal to cover the &
character and others as well should be trival.
To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:
function fixedEncodeURIComponent (str) { return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); }
Upvotes: 1