SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Grrr, how to achieve these triple nested quotes

    Code HTML:
    <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.

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code HTML4Strict:
    <a href="javascript:void(0)" onclick="playNewVideo('OdTGOa15vEw','The O.C. best music moment #14 - &quot;Champagne Supernova&quot;');">
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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)

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Code:
    foo="abc \"def\" ghi"
    the HTML parser will see a foo attribute with the value
    Code:
    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.
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I just realised there is another way. You can use numeric JavaScript escapes:
    Code HTML4Strict:
    <a href="javascript:void(0)" onclick="playNewVideo('OdTGOa15vEw','The O.C. best music moment #14 - \042Champagne Supernova\042');">
    or
    Code HTML4Strict:
    <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.
    Birnam wood is come to Dunsinane

  6. #6
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, thanks for the tips!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •