', ", \", then what?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alias

    #1

    ', ", \", then what?

    dynamically creating a number of thumbnails, which is working fine. However,
    they are supposed to call a function loadRightFrame when clicked, and I seem
    to be running into syntax problems. This code:


    pagecontent+='< img width="70" height="70" border="0" src="' + thumbArray[i]
    + '" onClick="loadRi ghtFrame(\"' + photoArray[i] + '\")">'

    resolves to this:

    pagecontent+='< img width="70" height="70" border="0"
    src="pictures/unt/kl1.jpg" onClick="loadRi ghtFrame("pictu res/unt/gr1.jpg")">

    I obviously cannot use double quotes OR single quotes when passing
    parameters to the loadRightFrame function. Escaping the quotes is not
    working either. Any ideas?


    Greets,

    A




  • Lasse Reichstein Nielsen

    #2
    Re: ', &quot;, \&quot;, then what?

    "Alias" <no@spam.org> writes:
    [color=blue]
    > pagecontent+='< img width="70" height="70" border="0" src="' + thumbArray[i]
    > + '" onClick="loadRi ghtFrame(\"' + photoArray[i] + '\")">'
    >
    > resolves to this:
    >
    > pagecontent+='< img width="70" height="70" border="0"
    > src="pictures/unt/kl1.jpg" onClick="loadRi ghtFrame("pictu res/unt/gr1.jpg")">[/color]

    Try:
    pagecontent+='< img width="70" height="70" border="0" src="' + thumbArray[i]
    + '" onClick="loadRi ghtFrame(\'' + photoArray[i] + '\')">'

    So it's: ', ", \', \\\", \\\\\', ...

    Try:
    ---
    var foo = 42;
    var s = '"\'\\\"\\\\\'f oo\\\\\'\\\"\'" ';
    alert(s);
    while (typeof s == "string") {
    s = eval(s);
    alert(s);
    }
    ---
    [color=blue]
    > I obviously cannot use double quotes OR single quotes when passing
    > parameters to the loadRightFrame function. Escaping the quotes is not
    > working either. Any ideas?[/color]

    Escape single quotes.

    When in doubt about something like this, I prefer to move backwards.
    Start with the desired result, and then add quotes around it add
    appropriate escapes.

    Start with valid HTML (except the dynamic parts, which we mark so
    we can remember where they are):
    ---
    <img width="70" height="70" border="0"
    src="{thumbArra y[i]}" onClick="loadRi ghtFrame('{phot oArray[i]}')">
    ---
    Then put it in quotes of some sort. Since the string contains fewest
    single quotes, that's what we'll use. That means that existing
    single quotes must be escaped (if put double quotes around it, then
    we would have to escape all double quotes). If there were any
    backslashes, they would have to be escaped as well.
    ---
    '<img width="70" height="70" border="0"' +
    ' src="{thumbArra y[i]}" onClick="loadRi ghtFrame(\'{pho toArray[i]}\')">'
    ---
    Now the valid HTML has become a valid Javascript string. We can then
    insert the dynamic parts, and the surrounding context:
    ---
    pagecontent += '<img width="70" height="70" border="0"' +
    ' src="'+thumbArr ay[i]+'" onClick="loadRi ghtFrame(\''+ph otoArray[i]+'\')">'
    ---


    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: ', &quot;, \&quot;, then what?

      Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
      [color=blue]
      > Try:
      > ---
      > var foo = 42;
      > var s = '"\'\\\"\\\\\'f oo\\\\\'\\\"\'" ';[/color]

      Actually, following the method I gave below, this would become:
      ---
      var s = '"\'\\"\\\\\'fo o\\\\\'\\"\'"';
      ---
      which is two needless backslashes shorter.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Alias

        #4
        Re: ', &quot;, \&quot;, then what?

        Lasse Reichstein Nielsen wrote:
        || Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
        ||
        ||| Try:
        ||| ---
        ||| var foo = 42;
        ||| var s = '"\'\\\"\\\\\'f oo\\\\\'\\\"\'" ';
        ||
        || Actually, following the method I gave below, this would become:
        || ---
        || var s = '"\'\\"\\\\\'fo o\\\\\'\\"\'"';
        || ---
        || which is two needless backslashes shorter.
        ||
        || /L
        || --
        || Lasse Reichstein Nielsen - lrn@hotpop.com
        || DHTML Death Colors:
        || <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html> 'Faith
        || without judgement merely degrades the spirit divine.'

        Thanks Lasse, working now.


        Comment

        Working...