Grrr, how to achieve these triple nested quotes


<a href="javascript:void(0)" onclick="playNewVideo('OdTGOa15vEw','The O.C. best music moment #14 - "Champagne Supernova"');">

<a href="javascript:void(0)" onclick="playNewVideo('OdTGOa15vEw','The O.C. best music moment #14 - \\"Champagne Supernova\\"');">


This doesnt work. :mad:

<a href="javascript:void(0)" onclick="playNewVideo('OdTGOa15vEw','The O.C. best music moment #14 - &quot;Champagne Supernova&quot;');">

Thank you kind sir. Is this the only way to do it? Like, escapes can’t be used to solve it? (just out of curiosity)

The script code is embedded in an HTML attribute value. Therefore it’s HTML’s rules you have to obey with the string as a whole. You escape double quotes as &quot; (or &#38;#34;) in HTML, and single quotes as &#38;#39;.

Using JavaScript escapes (\\" or \\') won’t do, because they’re not recognised by the HTML parser. If you write

foo="abc \\"def\\" ghi"

the HTML parser will see a foo attribute with the value

abc \\

followed by a bunch of invalid characters.

FWIW, the best practice is to avoid inline scripts altogether. The markup you showed is anything but unobtrusive, and it ties the behaviour very tightly to the content. A user with JavaScript disabled (or unsupported) will see a link, but it won’t work. Things that require JavaScript to work should be created unobtrusively with JavaScript. That way a non-JS user won’t have to see stuff that doesn’t work anyway.

Actually, I just realised there is another way. You can use numeric JavaScript escapes:

<a href="javascript:void(0)" onclick="playNewVideo('OdTGOa15vEw','The O.C. best music moment #14 - \\042Champagne Supernova\\042');">

or

<a href="javascript:void(0)" onclick="playNewVideo('OdTGOa15vEw','The O.C. best music moment #14 - \\u0022Champagne Supernova\\u0022');">

These will be handled by the JavaScript engine, and don’t interfere with the HTML quotes.

Cool, thanks for the tips!