Backslash confusion

Discussion in 'Javascript' started by freelance71, Apr 16, 2008.

  1. freelance71

    freelance71 Guest

    I have to pass the string '\abcd' to a function. Ofcourse one (or two) extra
    '\' is needed to escape but experimenting with it shows that I have to pass
    three extra backslashes to make it work.

    <script>
    var q = "\\\\abcd";
    displayLatex(q);
    </script>


    /* this function is in another file */

    function displayLatex(q) {


    alert(q); /* this shows \abcd when called by above script*/

    }


    Can anyone explain why this is so? why 4 backslashes?
    freelance71, Apr 16, 2008
    #1
    1. Advertising

  2. freelance71

    Erwin Moller Guest

    freelance71 schreef:
    > I have to pass the string '\abcd' to a function. Ofcourse one (or two) extra
    > '\' is needed to escape but experimenting with it shows that I have to pass
    > three extra backslashes to make it work.
    >
    > <script>
    > var q = "\\\\abcd";
    > displayLatex(q);
    > </script>
    >
    >
    > /* this function is in another file */
    >
    > function displayLatex(q) {
    >
    >
    > alert(q); /* this shows \abcd when called by above script*/
    >
    > }
    >
    >
    > Can anyone explain why this is so? why 4 backslashes?
    >
    >


    I do not understand.
    Your script alerts \\abcd over here (on FF), as expected.
    There must be more to it than you describe.

    Regards,
    Erwin Moller
    Erwin Moller, Apr 16, 2008
    #2
    1. Advertising

  3. freelance71

    freelance71 Guest

    "Erwin Moller"
    <> wrote in
    message news:4805abe9$0$14343$4all.nl...
    > freelance71 schreef:
    >> I have to pass the string '\abcd' to a function. Ofcourse one (or two)
    >> extra '\' is needed to escape but experimenting with it shows that I have
    >> to pass three extra backslashes to make it work.
    >>
    >> <script>
    >> var q = "\\\\abcd";
    >> displayLatex(q);
    >> </script>
    >>
    >>
    >> /* this function is in another file */
    >>
    >> function displayLatex(q) {
    >>
    >>
    >> alert(q); /* this shows \abcd when called by above script*/
    >>
    >> }
    >>
    >>
    >> Can anyone explain why this is so? why 4 backslashes?

    >
    > I do not understand.
    > Your script alerts \\abcd over here (on FF), as expected.
    > There must be more to it than you describe.
    >
    > Regards,
    > Erwin Moller



    can it be because I'm using it in a PHP file like this?

    <?php

    echo <<< HTMLOUT

    <script>

    var q = '\\\\abcd';

    displayLatex(q);

    </script>

    HTMLOUT;

    ?>
    freelance71, Apr 16, 2008
    #3
  4. freelance71

    Erwin Moller Guest

    freelance71 schreef:
    > "Erwin Moller"
    > <> wrote in
    > message news:4805abe9$0$14343$4all.nl...
    >> freelance71 schreef:
    >>> I have to pass the string '\abcd' to a function. Ofcourse one (or two)
    >>> extra '\' is needed to escape but experimenting with it shows that I have
    >>> to pass three extra backslashes to make it work.
    >>>
    >>> <script>
    >>> var q = "\\\\abcd";
    >>> displayLatex(q);
    >>> </script>
    >>>
    >>>
    >>> /* this function is in another file */
    >>>
    >>> function displayLatex(q) {
    >>>
    >>>
    >>> alert(q); /* this shows \abcd when called by above script*/
    >>>
    >>> }
    >>>
    >>>
    >>> Can anyone explain why this is so? why 4 backslashes?

    >> I do not understand.
    >> Your script alerts \\abcd over here (on FF), as expected.
    >> There must be more to it than you describe.
    >>
    >> Regards,
    >> Erwin Moller

    >
    >
    > can it be because I'm using it in a PHP file like this?


    Yes. That is the reason.

    >
    > <?php
    >
    > echo <<< HTMLOUT
    >
    > <script>
    >
    > var q = '\\\\abcd';


    that becomes:
    var q = '\\abcd';

    in your HTML.
    Simply check your source in your browser to see it.

    Regards,
    Erwin Moller

    >
    > displayLatex(q);
    >
    > </script>
    >
    > HTMLOUT;
    >
    > ?>
    >
    >
    Erwin Moller, Apr 16, 2008
    #4
  5. freelance71 wrote:
    >> freelance71 schreef:
    >>> I have to pass the string '\abcd' to a function. Ofcourse one (or two)
    >>> extra '\' is needed to escape but experimenting with it shows that I have
    >>> to pass three extra backslashes to make it work.

    > [...]
    > can it be because I'm using it in a PHP file like this?
    >
    > <?php
    >
    > echo <<< HTMLOUT
    >
    > <script>
    >
    > var q = '\\\\abcd';
    >
    > displayLatex(q);
    >
    > </script>
    >
    > HTMLOUT;
    >
    > ?>


    Yes, you have to escape each backslash for use with `echo'. A simple test
    (with php -a) shows that

    <?php

    echo <<<HTML

    var q = '\\a';

    HTML;

    ?>

    displays only one backslash.

    The simple and most efficient solution is to take heed of this general
    advice: Do not let the PHP parser do things it does not have to.

    <?php
    ...
    ?>
    var q = '\\a';
    <?php
    ...
    ?>

    (The first and last part are optional, of course.)


    F'up2 cl.php

    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Apr 16, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prabh
    Replies:
    2
    Views:
    494
    Chris Smith
    May 13, 2004
  2. Razvan

    slash vs backslash

    Razvan, Jul 7, 2004, in forum: Java
    Replies:
    7
    Views:
    17,451
    Razvan
    Jul 8, 2004
  3. Skinny Guy
    Replies:
    1
    Views:
    1,642
  4. Dennis Marks

    Slash and Backslash

    Dennis Marks, May 16, 2004, in forum: HTML
    Replies:
    5
    Views:
    14,223
    Hywel
    May 16, 2004
  5. Vincent Texier
    Replies:
    3
    Views:
    2,262
    Vincent Texier
    Jul 1, 2004
Loading...

Share This Page