Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping apostrophe

I have this simple script: onclick='sharemusic("Say It Ain't So", "Weezer");'

Because of the apostrophe on "Ain't" it isn't working properly. How do I escape the apostrophe without affecting the code's functionality?

Thanks a heap!

like image 831
seanlevan Avatar asked Jul 15 '13 18:07

seanlevan


1 Answers

I was reluctant to add this answer, because all the others are good and will be more useful to most people - by answering the underlying question: "How to escape in Javascript inside HTML attributes" - this won't answer that.

Some will consider the following a workaround, others will look at it as the only proper solution™.

Get rid of the typewriter apostrophe - although typewriters are cool and fashionable in all their retro grandeur, we can do better than that and go even older: Back to the proper, one-size-fits-one, typographic apostrophe (AKA "the curly apostrophe").

As a side effect, this will also fix the escaping problem:

onclick="sharemusic('Say It Ain’t So', 'Weezer');"

The typographers will be happy, and the browser will be happy. Well, as long as you're using UTF-8 like you should - or some other encoding that includes the typographic apostrophe. Unicode: U+2019, entity: ’ or ’ (yes, it shares its codepoint with the right single quotation mark - but it's still the "proper" apostrophe).

As mentioned in the comments, there are various libraries and CMS/blog plugins that help dealing with conversion to proper typographic symbols (and other typographic niceties). See wp-Typography, smartypants (which was, I think, the first implementation of something like this - developed by John Gruber, the original author of Markdown), typogrify etc.

I'm not sure what, if any, more recent alternatives there are, since I long ago made my own (non-public, I must admit) C# implementation.

like image 75
JimmiTh Avatar answered Oct 04 '22 22:10

JimmiTh