string interpolation .... like perl

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • w i l l

    string interpolation .... like perl

    I am beginning JavaScript, expect to see me in here a bit more over
    the next few months :) I am currently writing some HTML with
    JavaScript. As you know HTML has many double quotes ( " ) when
    declaring attibute values, I know in some cases quotes are optional,
    but I like them.


    Here is something I've been playing with , it works, but in my
    opinion, it's ulgy...

    <script>
    var myArray = new Array("blue","g reen","red");
    for(var i = 0; i < myArray.length; i++) {
    document.write( "<input type=\"submit\" value=\"" + myArray[i] +"\"
    onClick=\"docum ent.bgColor='" + myArray[i] +"'\">");
    }
    </script>

    Is there any way to mix javascript variables in with HTML without
    having to excape all of the qoutes?

    a perlish solution would be the qq operatior. but I know of nothing in
    javascript. Is there anything?

    perlish example...

    document.write( qq` an example of " any text" ... `);


    TIA

    w i l l
  • Laurent Bugnion, GalaSoft

    #2
    Re: string interpolation .... like perl

    Hi,

    w i l l wrote:[color=blue]
    > I am beginning JavaScript, expect to see me in here a bit more over
    > the next few months :) I am currently writing some HTML with
    > JavaScript. As you know HTML has many double quotes ( " ) when
    > declaring attibute values, I know in some cases quotes are optional,
    > but I like them.
    >
    >
    > Here is something I've been playing with , it works, but in my
    > opinion, it's ulgy...
    >
    > <script>
    > var myArray = new Array("blue","g reen","red");
    > for(var i = 0; i < myArray.length; i++) {
    > document.write( "<input type=\"submit\" value=\"" + myArray[i] +"\"
    > onClick=\"docum ent.bgColor='" + myArray[i] +"'\">");
    > }
    > </script>
    >
    > Is there any way to mix javascript variables in with HTML without
    > having to excape all of the qoutes?[/color]

    JavaScript considers " (double-quotes) and ' (single-quotes) as
    equivalent (unlike other languages!) and allows you this kind of
    construction:

    var myString = 'This string says "Hello world"...';

    However, there are situations where escaping is not avoidable. You will,
    in your programmer's life, have to escape other characters too, like \\,
    \t, \n, \r etc... After a while, you will stop noticing it and be able
    to read escaped code as easily as unescaped ones. It's just a matter of
    experience. It's the same in most advances languages anyway.

    HTH,

    Laurent
    [color=blue]
    > a perlish solution would be the qq operatior. but I know of nothing in
    > javascript. Is there anything?
    >
    > perlish example...
    >
    > document.write( qq` an example of " any text" ... `);
    >
    >
    > TIA
    >
    > w i l l[/color]


    --
    Laurent Bugnion, GalaSoft
    Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch

    Comment

    • Douglas Crockford

      #3
      Re: string interpolation .... like perl

      Make that

      document.write( '<input type="submit" value=' + myArray[i].quote() + '
      onclick=' + ('document.bgCo lor=' + myArray[i].quote() + ';').quote() + '>');


      Comment

      Working...