Requirements:
- I have to
fetch
a URL that contains a single quote. - I am required to call a function via; e.g.
<a href="javascript:my_function('my_quote%27s.txt');">...</a>
.
After a lot of hassle of trying to properly encode a single quote (%27
) both Waterfox and Chrome both keep throwing errors. Chrome allowed me to see that the error was triggered because the browsers are taking it upon themselves to decode strings (when I have not programed them to) turning %27
in to the literal single quote character so it errors out before the function is even called (e.g. my_function('my_quotes's.txt')
with the quote being internally decoded and causing the obvious triple quote issue).
I could use PHP's htmlentities($file_name, ENT_QUOTES)
though I'd have to string replace '
which seems pointlessly convoluted.
- I am required to support the single quote and make the call via
javascript:
. - I'd like to avoid literal interpretation of encoded strings when calling functions.
- I'd like to minimize the fuss and just use an encoding that JavaScript won't complain about.
- No frameworks or libraries.
How do I properly encode a single quote in a manner that JavaScript won't take it upon itself to somehow internally decode it and throw errors?