Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Backslash confusion

Reply
Thread Tools

Backslash confusion

 
 
freelance71
Guest
Posts: n/a
 
      04-16-2008
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?


 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      04-16-2008
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
 
Reply With Quote
 
 
 
 
freelance71
Guest
Posts: n/a
 
      04-16-2008

"Erwin Moller"
<Since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote in
message news:4805abe9$0$14343$(E-Mail Removed)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;

?>


 
Reply With Quote
 
Erwin Moller
Guest
Posts: n/a
 
      04-16-2008
freelance71 schreef:
> "Erwin Moller"
> <Since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote in
> message news:4805abe9$0$14343$(E-Mail Removed)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;
>
> ?>
>
>

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-16-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Store Network Path in Properties File? Backslash Weirdness. Skinny Guy Java 1 11-08-2004 10:52 PM
slash vs backslash Razvan Java 7 07-08-2004 07:11 AM
how to replace double backslash with one backslash in string... Vincent Texier Python 3 07-01-2004 01:03 PM
Slash and Backslash Dennis Marks HTML 5 05-16-2004 09:07 PM
Regexp question: Trouble matching with backslash Prabh Java 2 05-12-2004 11:10 PM



Advertisments