Link to home
Start Free TrialLog in
Avatar of daniel_c
daniel_c

asked on

Easy question about quote

One of my code is like this:
<script language=Javascript>
...

desc  [ 0 ] = "<%=desc%>";

</script>

The problem is, 'desc' variable may contains single quote or double quote inside it.

How to handle this?
Avatar of bebonham
bebonham

you will need to write an escaping routine in your server side script:

basically this routine will find all occurances of " and replace them with \"

I don't think there is anyway javascript can help you because you are writing data into the javascript.  

i.e. javascript can not write javascript.


Bob
two options I can think of.
1. Is to make sure that you use OR double or single , but not both.

2. To change the quote to something else , and then translate it :


desc  [ 0 ] = "<%=desc%>";

after Server Job will look like :

desc  [ 0 ] = "the title of the book is Y%YmosheY%Y";

and then :
desc  [ 0 ]=desc  [ 0 ].replace("Y%Y","'");



or something like that.
Avatar of daniel_c

ASKER

hey, the 'desc' variable is derived from ASP. So, any modification I need to do in ASP code before I send it to Javascript?
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

DOH!  beat me to the punch :)
Just make sure that on the server Side you only put double or single , and in the Javascript , use the other type.

I've tried the same thing like what knighEKnight suggested, but it brought me to error.

Anyway, what is %Y%Y avner?

after I convert, it will be like this:

desc = "The black's family is doing good" and it made the whole Javascript code after it become error.
Why not use &quot; for double quotes and &acute; for single ones? &acute; isn't really a quote mark, but it's close enough. But the &quot; will definitely work fine for double quotes.
The \ before the quotes will work fine too... I usually use that for javascript variables.

And on that last one, it should be &.quot; and &.acute; -- don't use the periods, of course.
Hey, just figured it out that the problem was not lying on the quote, but on Carriage Return character instead.

Anyway, thanks friends for helping me, and I choose knightEKnight comment as an answer.